如何在Symfony3.4中清除Controller中的缓存?

时间:2018-01-09 10:33:24

标签: php symfony caching php-7.1 symfony-3.4

将Symfony从3.3移植到3.4后,我的功能无法正常工作(之前有效)。我必须清除控制器中的缓存,当我执行下面的命令时,函数返回错误。

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)。我没有其他服务器来检查问题是否仍然存在

2 个答案:

答案 0 :(得分:1)

我正在调用已经实施的Symphony命令来清除或预热缓存(在Symphony 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);
    }
}

答案 1 :(得分:0)

您应该向var /目录添加有效权限以访问缓存文件:

chmod ... var/ -R

从网上访问时使用的用户是www-data