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.

Fatal error: Uncaught RuntimeException: Unable to create the storage directory (/var/www/pierrefay_preprod/pierrefay/pierrefay/var/cache/prod/profiler/e9/e8). in /var/www/pierrefay_preprod/pierrefay/pierrefay/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php:141 Stack trace: #0 /var/www/pierrefay_preprod/pierrefay/pierrefay/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Profiler/Profiler.php(105): Symfony\Component\HttpKernel\Profiler\FileProfilerStorage->write(Object(Symfony\Component\HttpKernel\Profiler\Profile)) #1 /var/www/pierrefay_preprod/pierrefay/pierrefay/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php(113): Symfony\Component\HttpKernel\Profiler\Profiler->saveProfile(Object(Symfony\Component\HttpKernel\Profiler\Profile)) #2 /var/www/pierrefay_preprod/pierrefay/pierrefay/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/EventDispatcher.php(212): Symfony\Component\HttpKernel\EventListener\ProfilerListener->onKernel in /var/www/pierrefay_preprod/pierrefay/pierrefay/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php on line 99