How do I disable Laravel view cache?
Asked Answered
C

13

36

I have an exception in one of my views. However, instead of telling me the name of the view so I can find it and fix it, laravel says it is in app/storage/views/110a3ecc0aa5ab7e6f7f50ef35a67a8b, which is meaningless.

How do I disable this view caching, so that laravel uses and refers to the actual files?

Chen answered 12/9, 2014 at 16:59 Comment(3)
possible duplicate of Laravel and view caching in development -- can't see changes right awaySurmullet
@Surmullet that question refers to php caching, and is a completely separate issue. My question is specifically about laravel's built-in view caching system, and is not a duplicate.Chen
I must have misread your question. Laravel needs to compile your blade files before they get rendered, so I'm not sure you will be able to see which named view the error occurs in, but you can open the compiled view. See @Antonio's answer here https://mcmap.net/q/427691/-laravel-blade-debug-view-name-on-errorSurmullet
L
26

Out of the box? You can't. But you can extend the BladeCompiler class, overriding the method resposible for checking if the view has been expired:

class MyBladeCompiler extends BladeCompiler {

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

        return parent::isExpired($path);
    }

}

You'll need to replace the BladeCompiler instance in IoC container, with your own compiler:

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

And then you just need to create that key in your app/config/view.php file

<?php

return [

    'cache' => false,

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

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

];

Or, like I do here:

return [

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

];
Lampblack answered 12/9, 2014 at 20:47 Comment(4)
Where may I put the second portion of code? In the AppServiceProvider?Almeta
I tried to add in AppServiceProvider, but it doesn't call isExpired method. Also bindShared is renamed to singleton in 5.5Pulcheria
AppServiceProvider didn't work for me, either. The binding was overwritten by the core ViewServiceProvider. I overcame this by extending ViewServiceProvider with a custom class with a new registerBladeEngine() method, and replaced the core ViewServiceProvider with my custom class in the providers array in config/app.phpJiminez
Latest version of laravel support this by default, check below: https://mcmap.net/q/420528/-how-do-i-disable-laravel-view-cacheEarshot
F
16

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

CACHE_EXPIRE=-1
Finbur answered 5/10, 2019 at 1:6 Comment(1)
This does a lot more than disable view caching. That's worth mentioning. -1Comminate
E
14

In laravel > v9.7.0, you can add inside config/view.php:

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

Here is the PR: https://github.com/laravel/framework/pull/41859

Earshot answered 7/4, 2022 at 23:30 Comment(6)
Get to the top! ☝️ Thanks for answering here, and for the PR, of course. 🤘Comminate
this is not working in laravel10Duffy
I didn't need the environment check since my app only has one view - it's a Laravel/Inertia/React SPA stack. No need for a view cache that I can see. I just set 'cache' => falseGarrettgarrick
@Garrettgarrick I suppose its still working for you on laravel10? I ask because of the comment of user151496Earshot
@BernardWiesner Yes. I think his problem is that he needs to use config('app.env') instead of App::environment. In my case I'm not making a call to environment, but if I did I would use config('app.env'). Thanks Bernard.Garrettgarrick
@BernardWiesner In other words, your code should look like this for Laravel 10. 'cache' => config('app.env') == 'local' ? false : trueGarrettgarrick
A
6

Solution

open php.ini

opcache.revalidate_freq=0
opcache.fast_shutdown=0

change to this. restart apache.

Areola answered 12/6, 2015 at 21:18 Comment(2)
How does this relate to laravel?Chen
Then probably your problem was not laravel caching, but php caching - these settings relate to the php opcache, which is a different thing from the laravel view cache.Chen
F
4

check your .env file Change CACHE_DRIVER=file to CACHE_DRIVER=array

Formularize answered 30/4, 2019 at 12:35 Comment(0)
O
3

If you have artisan, it's easy to clear the cache

php artisan view:clear

If you don't have or don't want artisan (can't think why you wouldn't want it, it's very useful), you can from the root of your project do

cd storage/framework/views/
rm *.php
Outbreak answered 13/10, 2019 at 15:13 Comment(0)
T
0

You can clear cache in this way, as well:

// Clear cache in laravel
Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
    // return what you want
    return "Cache is cleared";
});
Therm answered 20/4, 2021 at 6:28 Comment(0)
M
0

Here is the full answer

Go to vendor/illuminate/BladeCompiler.php change these 2 lines

use Illuminate\View\Compilers\Compiler; class BladeCompiler extends Compiler implements CompilerInterface

with the following:

use App\Support\CustomCompiler; class BladeCompiler extends CustomCompiler implements CompilerInterface

in your app/support folder (or whatever structure you are using) create the following class

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

your blade config file will look like this

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

auto dump and run....

Mornings answered 3/1, 2022 at 16:45 Comment(0)
A
0

If you are using MAMP, disable OPCache under Preferences, General, PHP-Cahce. just select off. thank me later.

Aphorize answered 20/2, 2022 at 20:19 Comment(0)
F
-1

Although some would call this sketchy, this was the quickest and most minimal way to do this on a small application I was working on

On the controller(s) that my routes pointed to:

public function __construct()
{
    exec('php /full/path/to/artisan view:clear');
}
Fredela answered 22/8, 2017 at 0:44 Comment(3)
I would advise against this approach if performance and scalability are important goals of your project.Boone
I'd advise against ever using exec at all tbh.. but if you're desperate :)Fredela
You can now use the Artisan facade to avoid using exec: Artisan::call('view:clear')Blowfish
S
-1

Laravel Creates view cache file because it has been told to do that. In .env File you will come across cache_driver which has default property as file change it to array.

Superpower answered 17/1, 2019 at 7:38 Comment(2)
Please post an example snippet of the .env file.Papeete
The cache_driver property controls exactly what it says - driver used for cache. One should not ruin all cache like Redis to disable only the view cacheStellarator
I
-1

A bit late to the party, however. I had the same issue: the browser not reflecting changes to the php code.

Simple solution for me was:

set the clock on the server to the same time as the dev computer !

sudo date +%T -s "11:14:00"
Irrepressible answered 13/11, 2019 at 7:24 Comment(0)
T
-2

In development environment, I just add and modify the next:

  • bootstrap/start.php

    $env = $app->detectEnvironment(function(){return 'testing';});
    
  • app/config/testing/cache.php add in array

    'cache' => false,
    
  • app/config/view.php add in array

    'cache' => false,
    
Taw answered 29/12, 2014 at 15:57 Comment(1)
Does not work. This may be due to laravel version - I am using 4.1 currently; was this changed in a later version?Chen

© 2022 - 2024 — McMap. All rights reserved.