如何禁用Laravel视图缓存?

Ben*_*ird 20 caching laravel blade

我的一个观点中有一个例外.然而,laravel没有告诉我视图的名称,所以我可以找到并修复它app/storage/views/110a3ecc0aa5ab7e6f7f50ef35a67a8b,而不是没有意义.

如何禁用此视图缓存,以便laravel使用并引用实际文件?

Ant*_*iro 20

盒子外面?你不能.但是您可以扩展BladeCompiler类,覆盖方法resposible以检查视图是否已过期:

class MyBladeCompiler extends BladeCompiler {

    public function isExpired($path)
    {
        if ( ! \Config::get('view.cache'))
        {
            return true;
        }

        return parent::isExpired($path);
    }

}
Run Code Online (Sandbox Code Playgroud)

您需要使用自己的编译器替换IoC容器中的BladeCompiler实例:

$app = App::make('app'); // or just $app = app();

$app->bindShared('blade.compiler', function($app)
{
    $cache = $app['path.storage'].'/views';

    return new MyBladeCompiler($app['files'], $cache);
});
Run Code Online (Sandbox Code Playgroud)

然后你只需要在app/config/view.php文件中创建该密钥

<?php

return [

    'cache' => false,

    'paths' => [base_path().'/resources/views'],

    'pagination' => 'pagination::slider-3',

];
Run Code Online (Sandbox Code Playgroud)

或者,就像我在这里:

return [

    'cache' => in_array(App::environment(), ['production', 'staging']),

];
Run Code Online (Sandbox Code Playgroud)

  • 我可以把第二部分代码放在哪里?在 AppServiceProvider 中? (2认同)

Ber*_*ner 14

在 laravel > v9.7.0 中,您可以在里面添加config/view.php

'cache' => App::environment('local') ? false : true
Run Code Online (Sandbox Code Playgroud)

这是 PR: https: //github.com/laravel/framework/pull/41859

  • 到达顶峰!☝️ 感谢您在这里的回答,当然还有 PR。 (2认同)

Ric*_*ard 8

this worked for me... added this to the .env file

CACHE_EXPIRE=-1
Run Code Online (Sandbox Code Playgroud)

  • 这是最有效的答案。 (2认同)

小智 7

打开php.ini

opcache.revalidate_freq=0
opcache.fast_shutdown=0
Run Code Online (Sandbox Code Playgroud)

改变这个.重启apache.

  • 那么可能你的问题不是laravel缓存,而是php缓存 - 这些设置与php opcache有关,这与laravel视图缓存不同. (8认同)
  • 这与laravel有什么关系? (4认同)

Jos*_*kpe 5

检查您的 .env 文件将 CACHE_DRIVER=file 更改为 CACHE_DRIVER=array