Installation
composer require anaxago/assetic
Add the service provider to config/app.php:
'providers' => [
// ...
Anaxago\Assetic\AsseticServiceProvider::class,
],
Basic Configuration Publish the config file:
php artisan vendor:publish --provider="Anaxago\Assetic\AsseticServiceProvider"
Edit config/assetic.php to define asset directories (e.g., css, js, images).
First Use Case: Compiling CSS
Define a build task in app/Assetic/BuildTasks/CssBuildTask.php:
namespace App\Assetic\BuildTasks;
use Anaxago\Assetic\BuildTask;
class CssBuildTask extends BuildTask
{
protected $input = 'public/css/source/*.css';
protected $output = 'public/css/compiled';
protected $filters = ['cssrewrite', 'yui_css'];
}
Register the task in config/assetic.php:
'build_tasks' => [
App\Assetic\BuildTasks\CssBuildTask::class,
],
Run the build:
php artisan assetic:build
Asset Organization
resources/assets/css/, resources/assets/js/).mix-manifest.json (if migrating from Laravel Mix) as a reference for input/output paths.Filter Chains Combine filters for optimization:
protected $filters = [
'cssrewrite', // Rewrite URLs in CSS
'yui_css', // Compress CSS
'uglifyjs', // Minify JS (if applicable)
];
Dynamic Asset Loading Use Blade directives to inject compiled assets:
@asset('css/compiled/style.css')
Define the directive in app/Providers/AppServiceProvider.php:
Blade::directive('asset', function ($path) {
return "<?php echo asset('{$path}'); ?>";
});
Environment-Specific Builds
Override config per environment (e.g., config/assetic.local.php):
'debug' => env('APP_DEBUG', false),
Watch Mode for Development Use Artisan to watch files and auto-rebuild:
php artisan assetic:watch
mix-manifest.json logic with Assetic’s asset() helper.asset() helper to point to CDN URLs in production.Caching Issues
php artisan assetic:clear
debug: false in production to avoid recompiling on every request.Filter Order Matters
cssrewrite before compression (yui_css) to avoid broken URLs in minified output.File Permissions
storage/ and public/ directories are writable by the web server.Missing Dependencies
php-yaml for YAML-based configs) or Node.js tools (e.g., uglify-js) if using JS filters.-v for details:
php artisan assetic:build -v
$this->dryRun = true; // In BuildTask
Custom Filters Extend Assetic’s filter system by creating a custom filter class:
namespace App\Assetic\Filters;
use Anaxago\Assetic\Filter\FilterInterface;
class CustomFilter implements FilterInterface {
public function filter($content) {
return str_replace('foo', 'bar', $content);
}
}
Register it in config/assetic.php:
'filters' => [
'custom' => App\Assetic\Filters\CustomFilter::class,
],
Pre/Post-Build Hooks
Use Laravel’s events to trigger actions before/after builds:
// In AppServiceProvider::boot()
event(new AsseticBuilding());
Asset Versioning Manually append query strings for cache busting:
@asset("css/compiled/style.css?v={$assetVersion}")
How can I help you explore Laravel packages today?