@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.