问题
I want assetic to output compressed js and css to something like this:
v2.3.1/css/whatever.css
Currently this is how I dump my css and js for production: $ php app/console assetic:dump --env=prod --no-debug
. But they get dumped into css/ and js/, without the version.
I have read this but it seems to refer to images only, not css/js.
An important reason for doing this is for cache busting/invalidation.
回答1:
Yes, known issue... In our production workflow we ended up with such block in bin/vendors
script:
if (in_array('--env=dev', $argv)) {
system(sprintf('%s %s assets:install --symlink %s', $interpreter, escapeshellarg($rootDir . '/app/console'), escapeshellarg($rootDir . '/web/')));
system(sprintf('%s %s assetic:dump --env=dev', $interpreter, escapeshellarg($rootDir . '/app/console')));
system(sprintf('%s %s myVendor:assets:install --symlink ', $interpreter, escapeshellarg($rootDir . '/app/console')));
} else {
system(sprintf('%s %s assets:install %s', $interpreter, escapeshellarg($rootDir . '/app/console'), escapeshellarg($rootDir . '/web/')));
system(sprintf('%s %s assetic:dump --env=prod --no-debug', $interpreter, escapeshellarg($rootDir . '/app/console')));
system(sprintf('%s %s myVendor:assets:install ', $interpreter, escapeshellarg($rootDir . '/app/console')));
}
As you can see, we defined our console command, which installs assets into web folder after installing and dumping of Symfony's assets. In the MyVendorCommand
script we do something like this:
$version = $this->getContainer()->getParameter('your_version_parameter');
$assetsInstallCommand = $this->getApplication()->find('assets:install');
$commandOptions = $input->getOptions();
$assetsInstallArguments = array(
'command' => 'assets:install',
'target' => 'web/version-' . $version,
'--symlink' => $commandOptions['symlink']
);
$assetsInstallInput = new ArrayInput($assetsInstallArguments);
$returnCode = $assetsInstallCommand->run($assetsInstallInput, $output);
回答2:
Ho, its a big Symfony2 bug ! I am not sure anyone reported it !
My solution was to add an alias in the Nginx config, but your is def. cleaner & better.
回答3:
My workaround is to make a rewriteRule in .htaccess file in order to serve the real file, but accepting the full url with version number. Something like this ...
app/config/config.yml
[...]
engines: ['twig']
assets_version: 20140523 # numeric version
assets_version_format: "assets-%%2$s/%%1$s"
[...]
web/.htaccess
[...]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^assets-([0-9]*)/(.*)$ ./$2 [L]
[...]
来源:https://stackoverflow.com/questions/11895299/how-to-prepend-assets-version-to-css-and-js