4

after migration Symfony from 3.3 to 3.4, my function not working (it works before). I have to clear cache in controller, and when I execute command below, function returns error.

exec(sprintf(
  "php %s/bin/console cache:clear --env=prod",
  $this->getParameter('kernel.project_dir')
));

It returns something like that:

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

In addition I can tell You, that in dev environment it works properly. Also when project run localy and simulate prod env (in address bar I type app.php after localhost:8000). I haven't other server to check if problem still occured

4

2 回答 2

7

我正在调用一个已经实现的 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);
    }
}
于 2018-11-23T12:46:44.067 回答
0

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

chmod ... var/ -R

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

于 2018-01-09T10:53:58.083 回答