php 如何在 Symfony 2 中重新加载 Twig 缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16386925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:02:26  来源:igfitidea点击:

How reload Twig cache in Symfony 2

phpsymfonytwig

提问by Azhar

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.

我有一个使用 Symfony 2 框架在 PHP 中开发的应用程序。我更改了 HTML 文件,但刷新页面时未反映更改。

  1. I restarted the server. No luck.

  2. I tried to remove the Twig folder from the /protected/cache/page itself. This is not loading.

  1. 我重新启动了服务器。没运气。

  2. 我试图从/protected/cache/页面本身中删除 Twig 文件夹。这不是加载。

How can I reload the Twig cache?

如何重新加载 Twig 缓存?

Notes:

笔记:

  • I am using tomcat server to deploy.
  • I don't have the Symfony 2 command line configured on the server.
  • I am new to PHP.
  • 我正在使用 tomcat 服务器进行部署。
  • 我没有在服务器上配置 Symfony 2 命令行。
  • 我是 PHP 新手。

回答by Alain Tiemblo

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.

重点是:里面的所有文件app/cache/都可以自由删除,需要的时候重新生成。

If you really want to clear only twig cache :

如果您真的只想清除树枝缓存:

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

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

替换<environment>devprodtest根据您的要求。

回答by Link

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_reloadoption, it will be determined automatically based on the debugvalue.

创建新的 Twig_Environment 实例时,您可以传递一个选项数组作为构造函数的第二个参数。其中之一是auto_reload。使用 Twig 进行开发时,每当源代码更改时重新编译模板很有用。如果您不为该auto_reload选项提供值,它将根据该debug值自动确定。

Set auto_reloadto be true:

设置auto_reloadtrue

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

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

Twig 的开发人员文档:http: //twig.sensiolabs.org/doc/api.html#environment-options

回答by Manuel

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:

我遇到了类似的问题,但删除缓存文件夹对我的模板没有任何影响,我不知道为什么。现在似乎可以解决我的问题的是我的 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

TwigBundle 配置

Twig Environment Options

树枝环境选项

回答by j4r3k

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/其他类似缓存,删除 twig 的缓存文件夹不会刷新模板,因为 twig 缓存仅包含 .php 文件。您需要删除 Twig 的缓存文件夹 + 执行包含以下内容的 php 文件:

opcache_reset();

opcache_reset();

回答by Abdelghafour Ennahid

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

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

Change:

改变:

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

to:

到:

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

and clear the cache if you want.

并根据需要清除缓存。

回答by Yoann Kergall

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

如果您使用 OPcache,请确保opcache.validate_timestamps=0在开发环境中注释掉。

回答by Themer

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);
}