【问题标题】:problems when using cache clear command in symfony controller action method在 symfony 控制器操作方法中使用缓存清除命令时出现的问题
【发布时间】:2017-06-29 04:14:59
【问题描述】:

我尝试使用以下代码通过网络方式清除缓存,但我收到了响应消息:

// 使用调试清除开发环境的缓存 // true [Symfony\Component\Debug\Exception\ContextErrorException] 警告: ini_set():会话处于活动状态。您无法更改会话模式 le此时的ini设置cache:clear [--no-warmup] [--no-optional-warmers] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--]

我的代码如下:

/**
* @Route("/cache/clear", name="cache_clear")
*/
public function cacheClearAction( ) 
{
    $application = new Application($this->get('kernel'));
    $application->setAutoExit(false);//exit after run
    $input = new ArrayInput([
        'command' => 'cache:clear',
        '--env'   => 'dev',
    ]);
    $output = new BufferedOutput();
    $runCode = $application->run($input, $output);
    $content = $output->fetch();
    return new Response($content);
}

ps:my php.ini session.auto_start => Off => Off

【问题讨论】:

  • 我试试这个方法,效果很好! $input = new ArrayInput([ 'command' => 'cache:clear', '--env' => 'dev', '--no-warmup' => true ]);
  • 接受您的回答,解释问题所在。
  • 我读过documnet(symfony.com/doc/current/reference/…) # 3.3版新功能:从Symfony 3.3开始,不推荐使用cache:clear命令的预热部分。您必须始终将 --no-warmup 选项传递给 cache:clear 并使用 cache:warmup 来预热缓存。

标签: php symfony caching


【解决方案1】:
public function cacheClearAction( ) 
{
    $application = new Application($this->get('kernel'));
    $application->setAutoExit(false);//exit after run
    $input = new ArrayInput([
        'command' => 'cache:clear',
        '--env'   => 'dev',
        '--no-warmup' => true
    ]);
    $output = new BufferedOutput();
    $runCode = $application->run($input, $output);
    $content = $output->fetch();
    return new Response($content);
}

【讨论】:

    【解决方案2】:

    这项工作,但我的回报错误

    public function cacheClearAction( ) 
    {
        $application = new Application($this->get('kernel'));
        $application->setAutoExit(false);//exit after run
        $input = new ArrayInput([
            'command' => 'cache:clear',
            '--env'   => 'dev',
            '--no-warmup' => true
        ]);
        $output = new BufferedOutput();
        $runCode = $application->run($input, $output);
        $content = $output->fetch();
        return new Response($content);
    }
    
    
    // Clearing the cache for the dev environment with debug // true // Warming up cache... 09:58:05 DEBUG [php] Warning: unlink(/application/var/cache/de_/Container9daLmfA.legacy): No such file or directory [ "exception" => Symfony\Component\Debug\Exception\SilencedErrorContext { +count: 1 -severity: E_WARNING trace: { /application/vendor/symfony/http-kernel/Kernel.php:746 { …} /application/vendor/symfony/http-kernel/Kernel.php:566 { …} } } ] // Removing old cache directory... // Finished [OK] Cache for the "dev" environment (debug=true) was successfully cleared.
    

    所以我用

    exec("pwd;  cd ./../; pwd; php bin/console cache:clear --env=prod",$content);
    

    在 Symfony 4 中 - pwd 是 /public :)

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 2010-11-13
      • 2013-03-26
      • 2013-02-18
      • 1970-01-01
      • 2017-12-21
      • 2014-08-22
      • 2016-05-09
      • 2012-06-07
      相关资源
      最近更新 更多