Provides a asset version strategy based on the file modification time for the Symfony Asset Component.
This only works if the web accessible files are stored locally.
This package is not on Packagist, so the repository will need to be added manually in composer.json
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ php composer.phar require xm/asset-version
This command requires Composer.
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new XM\AssetVersionBundle\XMAssetVersionBundle(),
);
// ...
}
}
Create a service for the version strategy:
assets.version_strategy:
class: XM\AssetVersionBundle\Asset\VersionStrategy\FileModifiedTimestampVersionStrategy
arguments: ['%web_root%']
%web_root%
is the root of the web directory where assets are stored. This will
be added to the paths passed to the version strategy. It could be configured as
parameter.web_root: '%kernel.root_dir%/../html'
where html
is the root of
the web accessible directory.
In config.yml
add/set:
framework:
assets:
version_strategy: assets.version_strategy
Add something similar to the following to your Apache site config or .htaccess
file. This will allow for versioning of CSS, JS, image and pdf files.
<IfModule mod_rewrite.c>
RewriteEngine On
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Filename-based cache busting
# Apply to CSS, JS, image and PDF files
# Uses the format /version-4.3/css/public.css (version can be 4.3, 4, or a timestamp)
RewriteRule ^version-([\d\.]+)/(.+)\.(css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest|pdf|map)$ $2.$3 [L]
</IfModule>