如何禁用 Laravel 视图缓存?

问题描述 投票:0回答:13

我的一个观点有一个例外。然而,laravel 没有告诉我视图的名称以便我找到它并修复它,而是说它在

app/storage/views/110a3ecc0aa5ab7e6f7f50ef35a67a8b
中,这是没有意义的。

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

caching laravel laravel-blade
13个回答
26
投票

开箱即用?你不能。但是您可以扩展 BladeCompiler 类,覆盖负责检查视图是否已过期的方法:

class MyBladeCompiler extends BladeCompiler {

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

        return parent::isExpired($path);
    }

}

您需要用您自己的编译器替换 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);
});

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

<?php

return [

    'cache' => false,

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

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

];

或者,就像我在这里做的那样:

return [

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

];

13
投票

这对我有用...将其添加到 .env 文件

CACHE_EXPIRE=-1

10
投票

在laravel > v9.7.0中,可以在里面添加

config/view.php
:

'cache' => App::environment('local') ? false : true

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


6
投票

解决方案

打开 php.ini

opcache.revalidate_freq=0
opcache.fast_shutdown=0

改成这个。重启apache。


4
投票

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


2
投票

如果你有

artisan
,很容易清除缓存

php artisan view:clear

如果你没有或者不想

artisan
(想不通你为什么不想要它,它非常有用),你可以从你的项目的根部做

cd storage/framework/views/
rm *.php

0
投票

Laravel 创建视图缓存文件,因为它已被告知要这样做。在

.env
文件中,您会遇到
cache_driver
具有默认属性,因为
file
将其更改为
array
.


0
投票

您也可以通过这种方式清除缓存:

// Clear cache in laravel
Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
    // return what you want
    return "Cache is cleared";
});

0
投票

这里是完整答案

转到 vendor/illuminate/BladeCompiler.php 改变这两行

使用 Illuminate\View\Compilers\Compiler; 类 BladeCompiler extends Compiler implements CompilerInterface

具有以下内容:

使用 App\Support\CustomCompiler; 类 BladeCompiler 扩展 CustomCompiler 实现 CompilerInterface

在你的 app/support 文件夹中(或者你正在使用的任何结构) 创建以下类

命名空间应用\支持;

use Illuminate\View\Compilers\Compiler;

class CustomCompiler extends Compiler {

    public function isExpired($path) {

        if ( !\config('blade.use_cache')) 
               return true;

        return parent::isExpired($path);
    }
}

你的刀片配置文件看起来像这样

return [
    'use_cache' => false,
    'cache' => storage_path('cache'),
    'views' => resources_path('views')
];

自动转储和运行....


0
投票

如果您使用的是 MAMP,请在 Preferences、General、PHP-Cahce 下禁用 OPCache。只需选择关闭。稍后谢谢我。


-1
投票

虽然有些人会称之为粗略,但这是在我正在处理的小型应用程序上执行此操作的最快和最简单的方法

在我的路线指向的控制器上:

public function __construct()
{
    exec('php /full/path/to/artisan view:clear');
}

-1
投票

然而,聚会有点晚了。 我有同样的问题:浏览器没有反映对 php 代码的更改。

对我来说简单的解决方案是:

将服务器上的时钟设置为与开发计算机相同的时间!

sudo date +%T -s "11:14:00"

-3
投票

在开发环境中,我只是添加修改了下:

  • bootstrap/start.php

    $env = $app->detectEnvironment(function(){return 'testing';});
    
  • app/config/testing/cache.php
    添加数组

    'cache' => false,
    
  • app/config/view.php
    添加数组

    'cache' => false,
    
© www.soinside.com 2019 - 2024. All rights reserved.