How reload Twig cache in Symfony 2

后端 未结 8 888
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 17:51

I have a application which is developed in PHP using the Symfony 2 framework. I have changed a HTML file, but the change is not reflecting when I refresh the page.

相关标签:
8条回答
  • 2020-12-13 17:51

    You can use the Symfony console to clear cache ./bin/console cache:clear

    0 讨论(0)
  • 2020-12-13 17:52

    I had a similar problem, but deleting the cache-folder did not have any impact on my template and I don't know why. What seems to solve my problem now is the following code in my config_dev.yml:

    twig:
        cache: false
    

    Maybe this is also a solution for you, so that you don't need to use the command all the time.

    References:

    TwigBundle Configuration

    Twig Environment Options

    0 讨论(0)
  • 2020-12-13 17:57

    If you are using opcache/other similar caching, deleting twig's cache folder won't refresh templates as twig cache consist of only .php files. You need to delete twig's cache folder + execute php file which contains:

    opcache_reset();

    0 讨论(0)
  • 2020-12-13 18:03

    you can add a function like this :

    public function renderView($view, array $parameters = array())
    {
        $loader = new \Twig_Loader_Filesystem($this->container->getParameter("template_path"));
        $twig = new \Twig_Environment($loader, array('auto_reload' => true,
            'cache' => false
        ));
    
        /////////////////////add a translate filter/////////////////////// 
        $getTextdomain = new \Twig_SimpleFilter('trans',function ($string){
            return $this->container->get('translator')->trans($string);
        });
    
        $twig->addFilter($getTextdomain);
        //////////////////////////////////////////////////////////////////
    
        ///////////////////////////Add an extension twig//////////////////
        $twig->addExtension(new Extension());
        //////////////////////////////////////////////////////////////////
    
        return $twig->render($view, $parameters);
    }
    
    0 讨论(0)
  • 2020-12-13 18:05

    When creating a new Twig_Environment instance, you can pass an array of options as the constructor second argument. One of them is auto_reload. When developing with Twig, it's useful to recompile the template whenever the source code changes. If you don't provide a value for the auto_reload option, it will be determined automatically based on the debug value.

    Set auto_reload to be true:

    $twig = new Twig_Environment($loader, array('auto_reload' => true));
    

    Twig's documentation for developers: http://twig.sensiolabs.org/doc/api.html#environment-options

    0 讨论(0)
  • 2020-12-13 18:11

    The most simple way, type the command :

    rm -rf app/cache/*
    

    The point is: all files in app/cache/ can be removed freely, they are regenerated when needed.

    If you really want to clear only twig cache :

    rm -rf app/cache/<environment>/twig
    

    Replace <environment> by dev, prod, or test according to your requirements.

    0 讨论(0)
提交回复
热议问题