What is the best way to clean the cache without ru

2020-05-20 08:45发布

In the admin panel of my project, I programmed the ability to change the database name to use. I wrote the new database name in the parameters.ini, and after that the cache had to be cleaned to load the new config.

What is the best way to clean the cache without running the console command?

Or is there another best practise how to change the current db.

7条回答
贼婆χ
2楼-- · 2020-05-20 09:40

Although his answer is more/less obsolete, his recommendation is still useful today:

The answer is amazingly quite simple.

All cache files are stored in a single cache/ directory located in the project root directory.

So, to clear the cache, you can just remove all the files and directories under cache/. And symfony is smart enough to re-create the directory structure it needs if it does not exist.

Fabien Potencier, November 03, 2007

So following his recommendation I wrote a function that does exactly that:

/**
* @Route("/cache/clear", name="maintenance_cache_clear")
*/
public function cacheClearAction(Request $request)
{
    $kernel=$this->get('kernel');

    $root_dir = $kernel->getRootDir();

    $cache_dir = $kernel->getCacheDir();

    $success = $this->delTree($cache_dir);

    return new Response('...');
}

where delTree($dir_name) could be any function that removes recursively a directory tree. Just check the PHP rmdir function's User Contribution Notes, there are plenty of suggestions.

查看更多
登录 后发表回答