Johni
Johni

Reputation: 2959

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

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.

Upvotes: 13

Views: 47226

Answers (7)

Eugen Mihailescu
Eugen Mihailescu

Reputation: 3711

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.

Upvotes: -1

Stan Fad
Stan Fad

Reputation: 1234

i'm clearing cache this way:

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

https://gist.github.com/basdek/5501165

Upvotes: 0

Tofandel
Tofandel

Reputation: 3575

@HelpNeeder's answer is good but the ClearCache command is not using the --env option given. It is instead using the current kernel the application is running on.

So for it to work with another environnement than the environnement you call the controller from, you need to tweak the code a bit (I can't edit his answer, it's saying the edit queue is full) so here is a more complete answer:

//YourBundle:YourController

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\BufferedOutput;

public function clearCacheAction($env = 'dev', $debug = true)
{
    $kernel = new \AppKernel($env, $debug);

    $application = new Application($kernel);
    $application->setAutoExit(false);

    $input = new ArrayInput([
        'command' => 'cache:clear'
    ]);

    $output = new BufferedOutput();
    $application->run($input, $output);

    return $this->render('someTwigTemplateHere', array('output' => $output->fetch()));
}

Then you configure the routes :

cache_clear_dev:
    path:     /_cache/clear/dev
    defaults: { _controller: YourBundle:YourController:clearCache, env: 'dev', debug: true }

cache_clear_prod:
    path:     /_cache/clear/prod
    defaults: { _controller: YourBundle:YourController:clearCache, env: 'prod', debug: false }

And you can now output the result of the command using {{ output }} in your twig template.

If you have an access control, don't forget to set the permission for this route to a SUPER_ADMIN or something, you wouldn't want anyone other than an admin able to clear the cache.

Upvotes: 1

HelpNeeder
HelpNeeder

Reputation: 6490

Most recent best way (2017). I'm creating this in controller:

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Output\BufferedOutput;

/**
 * Refresh Cache
 */
public function refreshRoutes()
{
    $kernel = $this->container->get('kernel');

    $application = new Application($kernel);
    $application->setAutoExit(false);

    $input = new ArrayInput([
        'command' => 'cache:clear',
        '--env'   => 'prod',
    ]);

    $output = new BufferedOutput();
    $application->run($input, $output);
}

Upvotes: 4

PLoginoff
PLoginoff

Reputation: 11

My working action with rendering of Success page. It clear the cache at shutdown.

public function clearCacheAction(Request $request)
{ 
    $dir = $this->get('kernel')->getRootDir() . '/cache';
    register_shutdown_function(function() use ($dir) {
        `rm -rf $dir/*`;
    });
    return $this->render('SomeBundle:SomePart:clearCache.html.twig');
}

Note, you lose sessions if you didn't configure session: in config.yml.

Upvotes: 1

Pacufist
Pacufist

Reputation: 101

You can call this action to clear a cache:

/**
 * @Route("/cache/clear", name="adyax_cache_clear")
 */
  public function cacheClearAction(Request $request) {
    $input = new \Symfony\Component\Console\Input\ArgvInput(array('console','cache:clear'));
    $application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->get('kernel'));
    $application->run($input);
   }

Upvotes: 10

Samy Dindane
Samy Dindane

Reputation: 18706

You can use the console command via exec():

exec("php /my/project/app/console cache:clear --env=prod");

Or simply empty the cache/ folder if you don't want to use the console command.

Upvotes: 17

Related Questions