arubacao/asset-cdn
Composer 安装命令:
composer require arubacao/asset-cdn
包简介
Serve Laravel Assets from a Content Delivery Network (CDN)
README 文档
README
Serve Laravel Assets from a Content Delivery Network (CDN)
Introduction
This package lets you push, sync, delete and serve assets to/from a CDN of your choice e.g. AWS Cloudfront.
It adds helper methods mix_cdn() and asset_cdn().
Simple Illustration
>>> env('USE_CDN') => true
$ php artisan asset-cdn:sync
// head.blade.php <link rel="stylesheet" href="{{ mix_cdn('/css/app.css') }}">
<!-- Result --> <link rel="stylesheet" href="https://cdn.mysite.com/css/app.css?id=081861342e950012abe3">
Installation
Install this package via composer:
$ composer require arubacao/asset-cdn
The service provider is auto-discovered by supported Laravel versions.
Notes:
arubacao/asset-cdnsupports Laravel9,10,11,12, and13.- The minimum supported PHP version is
8.1; Laravel13requires PHP8.3or newer. - The CI matrix tests the supported Laravel versions with their compatible PHP, Testbench, and PHPUnit versions.
Configuration
1. Configure Filesystem
Only required if you plan to manage your assets via the provided commands: asset-cdn:push, asset-cdn:sync, asset-cdn:empty
arubacao/asset-cdn utilizes Laravel's Filesystem to push, sync, delete assets to/from the CDN of your choice.
Therefore, you have to configure and define a filesystem specific for CDN purposes.
Please follow the official documentation.
If you plan to use AWS S3/Cloudfront you can use this configuration:
// config/filesystems.php 'asset-cdn' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), 'bucket' => env('AWS_CDN_BUCKET'), ],
2. Publish Config File
$ php artisan vendor:publish --provider="Arubacao\AssetCdn\AssetCdnServiceProvider"
3. Edit cdn_url and filesystem.disk
// config/asset-cdn.php [ 'cdn_url' => 'https://cdn.mysite.com', 'filesystem' => [ 'disk' => 'asset-cdn', ], ]
4. Edit files in config/asset-cdn.php
Only required if you plan to manage your assets via the provided commands: asset-cdn:push, asset-cdn:sync, asset-cdn:empty
files always assumes a relative path from the public directoy
-
ignoreDotFiles
Excludes "hidden" directories and files (starting with a dot). -
ignoreVCS
Ignore version control directories. -
include
Any file that matches at least oneincluderule, will be included. No file is included by default.-
paths
Define paths that should be available on the CDN.
The following example will match any file in anyjsorcsspath it can find in thepublicdirectory.'include' => [ 'paths' => [ 'js', 'css' ], ] /* * This config would try to find: * '/var/www/html/public/js' * '/var/www/html/public/css' * but also any other 'js' or 'css' path e.g. * '/var/www/html/public/vendor/js' * '/var/www/html/public/vendor/css' * You could explicitly exclude paths later */
-
files
Define files that should be available on the CDN.
The following example will match any file that starts withjs/back.app.jsin thepublicdirectory.'include' => [ 'files' => [ 'js/app.js', ], ], /* * This config would try to find: * '/var/www/html/public/js/app.js' * but also any other file that matches the path + filename e.g. * '/var/www/html/public/vendor/js/app.js' * You could explicitly exclude these files later */
-
extensions
Define filetypes that should be available on the CDN.
The following example will match any file of type*.cssor*.jsin thepublicdirectory.'include' => [ 'extensions' => [ '*.js', '*.css', ], ],
-
patterns
Define patterns for files that should be available on the CDN.
The following example will match any file that starts with lettersaorbin thepublicdirectory./* * Patterns can be globs, strings, or regexes */ 'include' => [ 'patterns' => [ '/^[a-b]/i', // starting with letters a-b ], ],
-
-
exclude
Any file that matches at least oneexcluderule, will be excluded. Files that are excluded will never be included, even if they have been explicitly included. Rules are identical as described above.
5. Set Additional Configurations for Uploaded Files
filesystem.options are passed directly to the Filesystem
which eventually calls the underlying Storage driver e.g. S3.
Please refer to the corresponding storage driver documentation for available configuration options.
The following example is recommended for AWS S3.
// config/asset-cdn.php [ 'filesystem' => [ 'disk' => 'asset-cdn', 'options' => [ 'ACL' => 'public-read', // File is available to the public, independent of the S3 Bucket policy 'CacheControl' => 'max-age=31536000, public', // Sets HTTP Header 'cache-control'. The client should cache the file for max 1 year ], ], ]
6. Set Environment Variable USE_CDN
# .env USE_CDN=true # Enables asset-cdn USE_CDN=false # Disables asset-cdn (default)
Usage
Commands
Recommended
Sync assets that have been defined in the config to the CDN. Only pushes changes/new assets. Deletes locally removed files on CDN.
$ php artisan asset-cdn:sync
Pushes assets that have been defined in the config to the CDN. Pushes all assets. Does not delete files on CDN.
$ php artisan asset-cdn:push
Deletes all assets from CDN, independent from config file.
$ php artisan asset-cdn:empty
Serving Assets
Replace mix() with mix_cdn().
Replace asset() with asset_cdn().
Development
$ composer test
$ composer test:phpunit9
$ composer lint
$ composer analyse
Quality recommendations are tracked in QUALITY.md.
Credits:
Icon from www.flaticon.com
Unmaintained git repo by Vinelab for inspiration only
Todo's:
- Video Tutorial: How to use S3/Cloudfront
- Write test for
ignoreVCSfinder config - Write test for
ignoreDotFilesfinder config - Extend
CombinedFinderTest
arubacao/asset-cdn 适用场景与选型建议
arubacao/asset-cdn 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 191.72k 次下载、GitHub Stars 达 110, 最近一次更新时间为 2018 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cdn」 「laravel」 「content delivery network」 「AWS Cloudfront」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arubacao/asset-cdn 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arubacao/asset-cdn 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arubacao/asset-cdn 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block that displays featured content - large image, title, description and link.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
Portal Options Links
ZRCMS - Content management for version tracking and content
Automatically rewrites the URLs of media and assets to use a CDN
H5P Core functionality in PHP
统计信息
- 总下载量: 191.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 110
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-03
