Installation
composer require becklyn/assets-bundle
Register the bundle in config/bundles.php:
return [
// ...
Becklyn\AssetsBundle\BecklynAssetsBundle::class => ['all' => true],
];
Enable Routes (Dev Only)
Add to config/routes/dev/assets.yaml:
becklyn_assets:
resource: "@BecklynAssetsBundle/Resources/config/routes.yaml"
prefix: /
First Use Case
Place assets in assets/ (e.g., assets/js/app.js). Reference them in Twig:
<script src="{{ asset('js/app.js') }}"></script>
The bundle auto-generates hashed filenames (e.g., app.abc123.js) in public/assets/.
Asset Organization
assets/ (e.g., assets/css/main.scss, assets/js/vendor.js).assets/admin/, assets/vendor/).Twig Integration
{{ asset('path/to/file.ext') }} → Outputs hashed URL.{{ asset('path/to/file.ext', { version: '1.2.3' }) }} → Forces regeneration if version changes.{{ asset('bundles/YourBundle/asset.ext') }} for bundle assets.Build Automation
webpack.mix.js:
mix.js('assets/js/app.js', 'assets/js')
.sass('assets/scss/app.scss', 'assets/css');
Environment-Specific Config
Override defaults in config/packages/becklyn_assets.yaml:
becklyn_assets:
output_dir: '%kernel.project_dir%/public/build/assets' # Custom output
debug: '%kernel.debug%' # Disable hashing in dev
compressors:
- 'zopfli' # Enable compression
Symfony Controller Integration Generate URLs programmatically:
$url = $this->get('becklyn_assets.helper')->getUrl('js/app.js');
{{ stimulus_controller('hello_controller', { asset: asset('js/controllers/hello_controller.js') }) }}
vite:assets to preprocess files, then reference via asset().php bin/console cache:clear --env=prod --no-warmup
Public Directory Management
public/assets/. The bundle clears this directory on cache:clear.output_dir config to point to a custom location if needed.Debug Mode Quirks
debug: true, assets are served without hashing (e.g., app.js instead of app.abc123.js).debug: false in prod environments for long-term caching.Compression Dependencies
zopfli/gzip must be installed for compression to work. Test with:
zopfli --version
yui or closure compressors if zopfli fails.File Watching
nodemon or Laravel Mix’s --watch flag during development.Bundle Assets
src/YourBundle/Resources/public/), use:
{{ asset('bundles/YourBundle/path/to/file.ext') }}
assets/ manually or via a build step.URL Generation Edge Cases
asset() are relative to the assets/ root.
❌ {{ asset('../js/app.js') }} (fails)
✅ {{ asset('js/app.js') }} (works)?v=1.0)—use the version option instead.Asset Not Regenerated?
php bin/console cache:clear
php bin/console assets:clear
var/cache/dev/assets.json for generated hashes.404 Errors
public/assets/ permissions (chmod -R 755 public/assets).output_dir in config matches the actual directory.Compression Failures
var/log/dev.log for compressor errors. Reinstall dependencies:
composer require --dev zopfli/zopfli-php
Custom Processors
Extend asset processing by implementing Becklyn\AssetsBundle\Processor\ProcessorInterface:
class MyProcessor implements ProcessorInterface {
public function process(string $content, string $path): string {
return str_replace('foo', 'bar', $content);
}
}
Register in config/packages/becklyn_assets.yaml:
becklyn_assets:
processors:
- My\Bundle\Processor\MyProcessor
Custom Output Directories
Override output_dir per environment:
# config/packages/becklyn_assets_prod.yaml
becklyn_assets:
output_dir: '%kernel.project_dir%/public/build/assets'
Twig Extensions Add custom asset functions:
// src/Twig/AppExtension.php
class AppExtension extends \Twig\Extension\AbstractExtension {
public function getFunctions() {
return [
new \Twig\TwigFunction('custom_asset', [$this->get('becklyn_assets.helper'), 'getUrl']),
];
}
}
Use in Twig:
{{ custom_asset('js/app.js') }}
Event Listeners Hook into asset generation events:
// src/EventListener/AssetsListener.php
class AssetsListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
AssetsEvents::PRE_PROCESS => 'onPreProcess',
];
}
public function onPreProcess(AssetsEvent $event) {
$event->setContent(strtoupper($event->getContent()));
}
}
How can I help you explore Laravel packages today?