fisharebest / laravel-assets
Laravel 的资产管理
Requires
- illuminate/console: 5 - 8
- illuminate/contracts: 5 - 8
- illuminate/support: 5 - 8
- league/flysystem: ^1.0.5
- mrclay/minify: ^3.0.1
- tedivm/jshrink: ^1.3.1
Requires (Dev)
- league/flysystem-memory: ~1.0
- php-coveralls/php-coveralls: ~2.4
- phpunit/phpunit: 5.7.27 | 6.5.14 | 7.5.20 | ~8.5 | ~9.5
README
laravel-assets
为 Laravel 5 - 8 提供简单、灵活的资产管理。合并和压缩 CSS 和 JS 文件,使您的网站更快。
安装
将依赖项添加到 composer.json
composer require fisharebest/laravel-assets
从 Laravel 5.5 开始,包将自动发现。对于更早的版本,您必须在 config/app.php
中添加服务提供者和外观。
return [ 'providers' => [ Fisharebest\LaravelAssets\AssetsServiceProvider::class, ], 'aliases' => [ 'Assets' => Fisharebest\LaravelAssets\AssetsFacade::class, ], ]
创建一个配置文件,config/assets.php
,包含默认值。编辑此文件中的设置以匹配项目的目录结构。
$ php artisan vendor:publish --provider="Fisharebest\LaravelAssets\AssetsServiceProvider"
步骤 1. 如何添加资产
您通常会为需要它们的每个模板(布局、视图、部分等)添加资产。
<!-- resources/views/layouts/master.blade.php --> <?php Assets::add(['jquery', 'bootstrap', 'global.js', 'style.css', 'analytics.js']) ?> <!-- the rest of your view ... -->
<!-- resources/views/pages/list.blade.php --> <?php Assets::add('list.js') ?> <!-- the rest of your view ... -->
当然,您也可以在任何您选择的地方添加资产;控制器、助手等。
除了单个文件外,您还可以添加命名的文件集合。这些在 config/assets.php
中定义。
在您有依赖关系的情况下,应按加载顺序列出文件。例如,如果 list.js
依赖于 jQuery,您将指定 jQuery 在 list.js
之前。
<!-- resources/views/pages/list.blade.php --> <?php Assets::add(['jquery', 'list.js']) ?> <!-- the rest of your view ... -->
重复项将被忽略,因此您可以向每个使用 jQuery 的视图添加 jQuery,它只会渲染一次。
步骤 2. 如何渲染资产链接
通常在 <head>
元素中渲染 CSS 资产,并在 <body>
元素的末尾渲染 JS 资产。
<!-- resources/views/layouts/master.blade.php --> <html> <head> {!! Assets::css() !!} </head> <body> ... {!! Assets::js() !!} </body> </html>
但如果是…
如果我的资产没有 .js
或 .css
扩展名怎么办?
在添加资产时指定类型作为参数。例如,
Assets::add('http://example.com/script?parameter', 'js')
如果我想将我的资产分成单独的组怎么办?
在添加和渲染资产时指定组作为参数。
<!-- resources/views/layouts/master.blade.php --> <?php Assets::add('jquery.js') ?> <?php Assets::add('ie8.js', null, 'ie8') ?> <?php Assets::add('analytics.js', null, 'head-script') ?> <html> <head> ... <!--[if lte IE 8]>{!! Assets::js('ie8') !!}<![endif]--> {!! Assets::js('head-script') !!} </head> <body> ... {!! Assets::js() !!} </body> </html>
如果我想向 style/script 添加额外的属性怎么办?
将属性列表作为渲染函数的参数。
{!! Assets::css('print', ['media' => 'print']) !!} {!! Assets::js('analytics', ['async']) !!}
如果我的资产文件很小怎么办?
有一个配置选项 inline_threshold
。任何小于此字节数的资产文件都将内联渲染,从而节省 HTTP 请求。
如果我想在运行时更改配置怎么办?
配置可以随时更改。它仅在渲染资产时生效。
Assets::setGzipStatic(6); Assets::css(); // will create compressed assets
如果我想使用自己的压缩器怎么办?
编写自己的过滤器(实现 Fisharebest\LaravelAssets\Filters\FilterInterface
)并在配置文件 config/assets.php
中指定它。使用现有的过滤器作为模板。
如果我想使用 CDN 或无 cookie 域怎么办?
在 destination_url
设置中指定一个与 destination
中给出的文件夹对应的 URL。
// config/assets.php return [ 'destination' => 'min', // We create assets here 'destination_url' => 'http://my-cdn.com/min', // Users read assets from here ]
如果我需要将我的管道化资产复制到外部服务器怎么办?
编写自己的通知器(实现 Fisharebest\LaravelAssets\Notifiers\NotifierInterface
)并在配置文件 config/assets.php
中指定它。使用现有的通知器作为模板。
您可能将 destination_url
设置为您的 CDN 服务器,并添加一个通知器,将文件从 destination
文件夹复制到该服务器。
如果由于防火墙/代理问题而无法使用 file_get_contents() 该怎么办?
编写自己的加载器(实现 Fisharebest\LaravelAssets\Loaders\LoaderInterface
)并在配置文件 config/assets.php
中指定它。可以使用现有的加载器之一作为模板。
更新资产或更改过滤器后,我该如何删除旧文件?
有一个Artisan命令可以做到这一点。
php artisan assets:purge