将 Symfony 从 3.3 迁移到 3.4 后,我的功能不起作用(以前有效)。我必须清除 Controller 中的缓存,当我执行下面的命令时,函数返回错误。
exec(sprintf(
"php %s/bin/console cache:clear --env=prod",
$this->getParameter('kernel.project_dir')
));
它返回类似的东西:
Fatal error: require(): Failed opening required '/[...]/var/cache/prod/ContainerAcrshql/getTwig_ExceptionListenerService.php' (include_path='.:/usr/local/share/pear') in /[...]/var/cache/prod/ContainerAcrshql/appProdProjectContainer.php on line 764 Fatal error: require(): Failed opening required '/[...]/var/cache/prod/ContainerAcrshql/getSwiftmailer_EmailSender_ListenerService.php' (include_path='.:/usr/local/share/pear') in /[...]/var/cache/prod/ContainerAcrshql/appProdProjectContainer.php on line 764
另外我可以告诉你,在开发环境中它可以正常工作。此外,当项目运行 localy 并模拟 prod env(在地址栏中,我在 localhost:8000 之后键入 app.php)。我没有其他服务器来检查问题是否仍然发生
最佳答案
我正在调用一个已经实现的 Symfony 命令来清除或预热缓存(在 Symfony 4 上测试)。
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
class CommandController extends AbstractController
{
/**
*
* @Route("/command/cache/clear", name="command_cache_clear")
*/
public function command_cache_clear(KernelInterface $kernel)
{
return $this->do_command($kernel, 'cache:clear');
}
/**
*
* @Route("/command/cache/warmup", name="command_cache_warmup")
*/
public function command_cache_warmup(KernelInterface $kernel)
{
return $this->do_command($kernel, 'cache:warmup');
}
private function do_command($kernel, $command)
{
$env = $kernel->getEnvironment();
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array(
'command' => $command,
'--env' => $env
));
$output = new BufferedOutput();
$application->run($input, $output);
$content = $output->fetch();
return new Response($content);
}
}
关于php - 如何在 Symfony3.4 中清除 Controller 中的缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48166213/