在不运行控制台命令的情况下清理缓存的最佳方法是什么?

时间:2012-05-30 12:07:53

标签: php caching symfony

在我项目的管理面板中,我编写了更改要使用的数据库名称的功能。我在parameters.ini中编写了新的数据库名称,之后必须清理缓存以加载新的配置。

在不运行控制台命令的情况下清理缓存的最佳方法是什么?

或者是否有另一种最佳做法如何更改当前的数据库。

7 个答案:

答案 0 :(得分:16)

您可以通过exec()使用控制台命令:

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

如果您不想使用console命令,或者只是清空 cache / 文件夹。

答案 1 :(得分:10)

您可以调用此操作来清除缓存:

/**
 * @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);
   }

答案 2 :(得分:3)

最近的最佳方式(2017年)。我在控制器中创建了这个:

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);
}

答案 3 :(得分:1)

我在渲染Success页面时的工作。它会在关机时清除缓存。

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');
}

请注意,如果您未在config.yml中配置session:,则会丢失会话。

答案 4 :(得分:1)

@ HelpNeeder的答案很好,但ClearCache命令没有使用给定的--env选项。而是使用运行应用程序的当前内核。

因此,要使用其他环境而不是您调用控制器的环境,您需要稍微调整一下代码(我无法编辑他的答案,它说编辑队列已满)所以这里有一个更完整的答案:

//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()));
}

然后配置路由:

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 }

现在,您可以在树枝模板中使用{{output}}输出命令的结果。

如果您有访问控制权,请不要忘记将此路由的权限设置为SUPER_ADMIN或其他内容,您不希望管理员以外的任何人清除缓存。

答案 5 :(得分:0)

我正在以这种方式清除缓存:

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

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

答案 6 :(得分:-1)

尽管他的回答已经过时/过时,his recommendation今天仍然有用:

  

答案非常惊人   简单。

     

所有缓存文件都存储在位于的单个缓存/目录中   项目根目录。

     

因此,要清除缓存,您只需删除所有文件即可   缓存/下的目录。 symfony非常聪明,可以重新创建   如果它不存在,它需要的目录结构。

     

Fabien Potencier ,11月03日, 2007

因此,按照他的建议,我编写了一个完全正确的函数:

/**
* @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('...');
}

其中delTree($dir_name)可以是递归删除目录树的任何函数。只需查看PHP rmdir函数的用户贡献笔记there are plenty of suggestions

相关问题