gpt4 book ai didi

symfony2 从 Controller 重新编译容器

转载 作者:行者123 更新时间:2023-12-02 01:27:13 24 4
gpt4 key购买 nike

我想在使用 $this->container->compile();

时从 Controller 重新编译容器
public function changeAction(Request $request)
{
//......
echo($this->container->getParameter('mailer_user')."\n");
/*$cmd='php ../app/console cache:clear';
$process=new Process($cmd);
$process->run(function ($type, $buffer) {
if ('err' === $type) {
echo 'ERR > '.$buffer;
}
else {
echo 'OUT > '.$buffer;
}
});*/

$this->container->compile();
echo($this->container->getParameter('mailer_user')."\n");
die();
}

我收到一个错误:您无法编译转储的卡住容器

我想知道当我从 Controller 清除缓存时容器是否会重新编译?

最佳答案

如果您试图获取在请求期间已被修改的参数值,您可以这样做:

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

public function changeAction(Request $request)
{
$originalParam = $this->container->getParameter('mailer_user');

// Rebuild the container
$container = new ContainerBuilder();
$fileLocator = new FileLocator($this->getParameter('kernel.root_dir').'/config');

// Load the changed config file(s)
$loader = new PhpFileLoader($container, $fileLocator);
$loader->setResolver(new LoaderResolver([$loader]));
$loader->load('parameters.php'); // The file that loads your parameters

// Get the changed parameter value
$changedParam = $container->get('mailer_user');

// Or reset the whole container
$this->container = $container;
}

此外,如果您需要从 Controller 中清除缓存,还有一种更简洁的方法:

$kernel = $this->get('kernel');
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$application->setAutoExit(false);

$application->run(new \Symfony\Component\Console\Input\ArrayInput(
['command' => 'cache:clear']
));

关于symfony2 从 Controller 重新编译容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com