Installation
composer require alpixel/assetversionbundle
Ensure your composer.json has PHP ≥5.3.0 and Symfony <3.3 (due to bundle compatibility).
Register the Bundle
Add to app/AppKernel.php:
new Alpixel\Bundle\AssetVersionBundle\AlpixelAssetVersionBundle(),
First Use Case
gulp-rev) to generate versioned filenames (e.g., vendor.min.css → vendor.min.abc123.css).<link rel="stylesheet" href="{{ asset('css/vendor.min.css'|asset_version) }}">
|asset_version filter appends the version hash (e.g., vendor.min.abc123.css).Asset Pipeline
gulp-rev, webpack, or grunt to generate versioned assets (e.g., assets/css/style.css → assets/css/style.abc123.css).assets.json) for reference.Twig Template Usage
asset_version filter to dynamically append version hashes:
{% for file in ['style.css', 'script.js'] %}
<link href="{{ asset('css/' ~ file)|asset_version }}">
{% endfor %}
Symfony Asset Integration
asset() function for full path resolution:
<script src="{{ asset('js/app.min.js'|asset_version) }}"></script>
Dynamic Versioning
vendor.min.css).Symfony 3.3+ Incompatibility
stfalcon/tinymce-bundle for asset management.Version Hash Mismatch
vendor.min.abc123.css) doesn’t exist, the filter silently falls back to the original. Debug tip: Verify your asset pipeline generates correct hashes.gulp-rev) updates the manifest file (assets.json) with accurate mappings.Caching Headaches
Cache-Control: max-age=31536000) or a CDN to manage caching.symfony/webpack-encore for modern asset pipelines with cache-busting.Twig Filter Scope
asset_version filter only works in Twig templates. For PHP logic, use the underlying service:
$versionedPath = $this->get('alpixel_asset_version.twig_extension')->getVersionedPath('css/vendor.min.css');
Check Generated Paths:
{{ dump(asset('css/vendor.min.css'|asset_version)) }}
Verify the output matches your expected versioned filename.
Manifest Validation:
If using gulp-rev, ensure assets.json is in your web/ directory and includes all versioned files:
{
"css/vendor.min.css": "css/vendor.min.abc123.css"
}
Custom Version Sources
Override the default behavior by extending the bundle’s AssetVersionExtension:
// src/Alpixel/Bundle/AssetVersionBundle/DependencyInjection/Compiler/OverrideExtensionPass.php
public function process(ContainerBuilder $container) {
$extension = $container->getDefinition('alpixel_asset_version.twig_extension');
$extension->addMethodCall('setCustomVersionResolver', [new Callback('app.custom_version_resolver')]);
}
Dynamic Versioning For database-driven assets, create a custom Twig function:
// src/App/Twig/AppExtension.php
public function getFunctions() {
return [
new \Twig_SimpleFunction('dynamic_asset_version', [$this, 'getDynamicVersionedPath']),
];
}
Fallback Logic Modify the Twig extension to handle missing files differently (e.g., throw an exception):
// Override Alpixel\Bundle\AssetVersionBundle\Twig\AssetVersionExtension
public function getVersionedPath($path) {
if (!file_exists($this->getVersionedAssetPath($path))) {
throw new \RuntimeException("Versioned asset not found: {$path}");
}
return parent::getVersionedPath($path);
}
How can I help you explore Laravel packages today?