我試圖通過將version作爲參數引入到每個資產來解決Symfony 3中的緩存問題。我正在使用AsseticSymfony 3 - 如何在框架加載之前設置參數
# app/config/config.yml
parameters:
version: 'v1.0'
framework:
# ...
assets:
version: '%version%'
這工作正常。但是,問題在於,當我將某個版本部署到生產環境時,我必須手動編輯parameters.yml
。所以我需要在部署發生時每次都自動生成/更新。
我能想到的一種方法是根據文件的最後更改生成MD5字符串。所以,讓我們說如果我能夠得到一個版本。我想用版本替換參數。使用CompilerPass
,我可以添加version
參數。
//AppBundle/AppBundle.php
use AppBundle\DependencyInjection\Compiler\Version;
class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container); // TODO: Change the autogenerated stub
$container->addCompilerPass(new Version());
}
}
//AppBundle/DependencyInjection/Compiler/Version.php
namespace AppBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class Version implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->setParameter('version', uniqid());
}
}
uniqid()
作爲測試添加。但是,此代碼可以工作並添加參數「版本」,但在「框架」配置初始化後。由於這個原因,%version%
在「框架」塊下指出它找不到參數。
如何在「框架」初始化之前創建此參數?