繁体   English   中英

Symfony2 - 要查看twig中的更改,必须每次都清除缓存

[英]Symfony2 - to see changes in twig have to clear cache every-time

我现在才有这个问题。 每当我改变我的树枝时,我都要cache:clear 如果代码中出现错误,是否显示错误? 我该怎么办?!

我多次遇到过这个问题。 如果您的网站被这么多用户访问并清除了缓存。 我确定你的网站已经关闭几分钟,直到新缓存生成。

因此,生产服务器上的清除缓存不应该是常规活动。 从这个问题中可以克服几种解决方案或技巧:

  1. 找到您的网站流量较低的时间。 可能是在晚上的某个时间然后清除缓存。
  2. 如果要清除缓存,请设置生产服务器的副本,然后计划将公共域ip切换到新的副本以进行计时,以便用户无法面对停机时间并清除实际生产服务器上的缓存。 将公共域ip切换回生产服务器。
  3. 如果你在模板ietwig中做了一些更改,并希望在生产中进行更改。然后尝试在app / cache / prod / twig目录中找到模板并grep模板名称,你将获得文件。 移动文件或删除文件,您的更改将生效在生产服务器上。

如何清除缓存

php app/console cache:clear 
chmod -R 777 app/cache
chmod -R 777 app/logs 

替代

您必须在位于Web文件夹中的app.php文件中进行一些更改。

更改

   $kernel = new AppKernel('prod', false);    

  $kernel = new AppKernel('prod', true);

并清除缓存

我刚刚创建了一个控制台命令来有选择地手动列出或删除twig缓存文件,而不是运行耗时的clear:cache来清除所有内容。 语法是:

kmlf:twig --clear --env = dev AcmeBundle :: nglayout.html.twig AcmeBundle:Simple:simple3.html.twig

如果您只想列出缓存文件位置,可以删除--clear标志。 它似乎在Symfony 2.3的prod和dev环境中运行良好:

use Symfony\Component\Console\Command\Command; 
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;

class TwigCacheCommand extends ContainerAwareCommand
{

    public function configure()
    {
        $this->setName('kmlf:twig')
        ->setDescription('selectively manage the twig cache')

        ->addArgument(
            'names',
            InputArgument::IS_ARRAY,
               'Example AcmeBundle:Section:view.html.twig',
                null
        )->addOption('clear','c', InputOption::VALUE_NONE, 'delete cache files' );
    }

    public function write($output, $text) {
        $output->writeln($text);
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {

        $environment = $this->getContainer()->get('twig');
        $names = $input->getArgument('names');


        $actionName = null;
        if ($input->getOption('clear')) {
            $actionName = 'deleting';
            $action =  function ($fileName) {
                unlink($fileName);
            };
        } else {
            $actionName="path:";
            $action = function ($filename) {

            };
        }

        foreach ($names as $name) {

            $fileName = $environment->getCacheFilename($name);

            if (file_exists($fileName)) {
                $action($fileName);
            } else {
                $fileName = 'not found.';
            }
            $this->write($output, $actionName.' '.$name."\ncacheFile: ".$fileName);
        }
        $this->write($output, 'Done');
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM