Easily integrate Assetic with Laravel 4.
- Easily maintain assets within groups.
- Instant single file compilation, concatenation, and minification.
- Apply multiple filters to each group.
- Automatically updates output files when their inputs have been changed.
- Pre-compile assets using
artisan asset:warm
.
Add to your composer.json:
"require": {
"laravel/framework": "4.0.*",
"slushie/laravel-assetic": "dev-master",
"lmammino/jsmin4assetic": "1.0.0",
"leafo/lessphp": "0.4.0"
}
After running composer update
, you need to add the service provider (and optionally, alias the Asset
facade) in your app/config/app.php
file:
'providers' => array(
...
'Slushie\LaravelAssetic\LaravelAsseticServiceProvider',
...
),
'aliases' => array(
...
'Asset' => 'Slushie\LaravelAssetic\Facades\AssetFacade',
...
),
Once your app's configuration has been updated, generate the package config:
php artisan config:publish slushie/laravel-assetic
Now the laravel-assetic configuration file will be available at:
app/config/packages/slushie/laravel-assetic/config.php
Finally, edit the configuration file file to define your assets. You can define multiple groups, each with different filters and assets.
Filters are defined within the package configuration file.
'filters' => array(
'css_min' => 'Assetic\Filter\CssMinFilter',
'css_import' => 'Assetic\Filter\CssImportFilter',
'css_rewrite' => 'Assetic\Filter\CssRewriteFilter',
'embed_css' => 'Assetic\Filter\PhpCssEmbedFilter',
'less_php' => 'Assetic\Filter\LessphpFilter',
'js_min' => 'Assetic\Filter\JSMinFilter',
'coffee_script' => 'Assetic\Filter\CoffeeScriptFilter',
'yui_js' => function () {
return new Assetic\Filter\Yui\JsCompressorFilter('yui-compressor.jar');
},
),
Each group defines assets
and filters
as inputs, and an output
file that should be included in your view.
'groups' => array(
'main_js' => array(
'filters' => array(
'js_min',
),
'assets' => array(
'jquery', // Named asset defined below
'assets/js/common/search.js', // Single file
'assets/js/coolarize/*js', // Folder inclusion
),
'output' => 'scripts.js', // Writable output relative to public_path()
),
),
Once defined, your groups can then be accessed from within your views using the Asset
facade.
To link to the main_js
group, you can use the Asset::url()
method as follows:
<script src="<?php echo Asset::url('main_js'); ?>"></script>
This will output the URL to the asset file (in this example, /scripts.js
).
When the page loaded, Assetic will generate the file, joining all files and running the defined filters.
You can also generate the asset output files via the artisan command:
php artisan asset:warm
Of course, this can be performed as a composer post-install
command to generate assets at deployment time.
More information can be acquired by reading through the source, which is fully documented, or you may feel free to raise issues at https://github.com/slushie/laravel-assetic/issues