以编程方式清除symfony 2上的缓存

以编程方式清除symfony 2上的缓存

问题描述:

我试图通过我的应用程序清空Symfony 2缓存,就像我在做 php应用程序/控制台缓存:清除

I'm trying to empty the Symfony 2 cache through my application just as if I was doing php app/console cache:clear.

我尝试了许多不同的解决方案,例如:

I've tried many different solutions, such as:

//solution 1
$process = new Process('php '.__DIR__.'/../../../../../app/console cache:clear');
$process->setTimeout(3600);
$process->run();

//solution 2
$input = new StringInput(null);
$output = new NullConfigurationOutput();
$command = new CacheClearCommand();
$command->setContainer($container);
$command->run($input, $output);

//solution 3    
$container->get('cache_clearer')->clear($container->getParameter('kernel.cache_dir'));

我也尝试过 rm -Rf 缓存文件夹。

I've also tried to rm -Rf the cache folder.

由于我遇到很多错误,它们都无法正常工作。以下错误是我在解决方案2上遇到的错误(这似乎是最好的):

None of them is fully working as i'm getting many errors. The following error is the one i'm getting on solution 2 (which seemed to be the best):


无法启动会话:已由PHP启动(已设置$ _SESSION)。

Failed to start the session: already started by PHP ($_SESSION is set).

似乎Symfony试图在渲染期间重建页面或重新启动应用程序处理。

Seems like Symfony is trying to rebuild the page or reboot the application during the render process.

有人如何通过一些php代码清除缓存?

How could someone clear the cache via some php code?

编辑:
我是实际上由于我的路由系统而试图重新生成缓存。我制作了一个使用symfony 2路由的捆绑软件,并添加了出于SEO目的对其进行重写的可能性(例如:我有一个指向/ category的front_category路由,通过这个捆绑软件,我可以将其重写为category-seo-optimized。 html)。

EDIT : I'm actually trying to regenerate the cache because of my routing system. I've made a bundle which take a symfony 2 route and add the possibility to rewrite it for SEO purpose (example : i have a front_category route which point to /category, with this bundle i can rewrite it to category-seo-optimized.html).

如果我在需要特定参数(例如ID)的路线上,则需要为其改写一条特殊路线。例如,对于/ category / 3,我需要创建一个名为category_3的路由,并且..为了让symfony知道我已经更改了路由系统,我必须清除缓存。

If i'm on a route which needs particular parameter, such as an ID, i need to make a special route for it in order to rewrite it. For example, for /category/3 i need to create a route which is named category_3 and.. in order to let symfony knows i've changed the routing system, i had to clear the cache.

与我刚才遇到的问题完全相同。
对我来说最简单的解决方案是:

Exactly the same problem that I just had. Simplest solution for me was:

use Symfony\Component\Filesystem\Filesystem;

并在控制器中:

$fs = new Filesystem();
$fs->remove($this->container->getParameter('kernel.cache_dir'));

这将删除当前环境的缓存目录。

This will delete the cache directory for the current environment.