The 08/16/2016

Empty the cache via php on Symfony3

Today I need to clear my cache via php on Symfony3, here is a very simple solution. before, on Symfony2 my code to do this was :

     //clear cache
     $input = new StringInput(null);
     $output = new NullOutput();
     $command = new CacheClearCommand();
     $command->setContainer($this->container);
     $command->run($input, $output);

But when i upgrade to Symfony3 i had an error : ServiceNotFoundException in Container.php line 266 So i took some minutes to search for a solution and the shortest way i found was :

     $dir = $this->get('kernel')->getRootDir().'/..';
     exec("php $dir.'/bin/console cache:clear");

But obviously it still posed problems for me, so the solution I propose:

 //clear cache
 $fs = new Filesystem();
 $fs->remove($this->container->getParameter('kernel.cache_dir'));
    
If you have "cleaner" suggestions, do not hesitate to let a comment, I am here to learn. Pierre.
Comments on this article
No comments yet for this article. Be the first !

You must be logged in to comment an article.