How reload twig cache in symfony2

2019-01-22 06:19发布

I am new to PHP , I have a application which is developed in PHP and symfony2 framework. I have changed html file , the change is not reflecting when i am refreshing the page.

01. I restarted the server No luck.

02. I tried to remove the twig folder from /protected/cache/ page it self not loading.

Kindly advise , I am using tomcat server to deploy.

Note : I don't have symfony2 command line configured on server.

标签: php symfony twig
7条回答
Viruses.
2楼-- · 2019-01-22 06:21

If you are using OPcache make sure to comment out opcache.validate_timestamps=0 in dev environment.

查看更多
Luminary・发光体
3楼-- · 2019-01-22 06:23

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

查看更多
疯言疯语
4楼-- · 2019-01-22 06:25

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);
}
查看更多
老娘就宠你
5楼-- · 2019-01-22 06:30

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

查看更多
放荡不羁爱自由
6楼-- · 2019-01-22 06:33

You have to do some changes in app.php file located in web folder.

Change:

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

to:

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

and clear the cache if you want.

查看更多
男人必须洒脱
7楼-- · 2019-01-22 06:39

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.

查看更多
登录 后发表回答