如何将资产版本预置到 css 和 js


How to prepend assets version to css and js?

我希望assetic将压缩的js和css输出到这样的内容: v2.3.1/css/whatever.css

目前,这就是我转储 css 和 js 用于生产的方式:$ php app/console assetic:dump --env=prod --no-debug .但是它们被转储到 css/和 js/中,没有版本。

我已经阅读了这篇文章,但它似乎仅指图像,而不是 css/js。

这样做的一个重要原因是缓存破坏/失效。

是的,已知问题...在我们的生产工作流程中,我们最终在脚本中bin/vendors了这样的块:

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')));
}

如您所见,我们定义了控制台命令,该命令在安装和转储Symfony的资产后将资产安装到Web文件夹中。在MyVendorCommand脚本中,我们执行以下操作:

$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);

Ho,这是一个很大的Symfony2错误!我不确定有人报告了它!

我的解决方案是在 Nginx 配置中添加一个别名,但你的定义更干净、更好。

我的解决方法是在 .htaccess 文件中进行重写规则以提供真实文件,但接受带有版本号的完整 url。像这样的东西...

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]
[...]