34

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?

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
Benubird
  • 18,551
  • 27
  • 90
  • 141
  • possible duplicate of [Laravel and view caching in development -- can't see changes right away](http://stackoverflow.com/questions/20579182/laravel-and-view-caching-in-development-cant-see-changes-right-away) – RobbieP Sep 12 '14 at 19:34
  • 2
    @RobbieP 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. – Benubird Sep 16 '14 at 10:36
  • 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 http://stackoverflow.com/a/19866546/942846 – RobbieP Sep 16 '14 at 11:14

13 Answers13

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']),

];
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • 2
    Where may I put the second portion of code? In the AppServiceProvider? – phaberest May 09 '16 at 14:45
  • I tried to add in AppServiceProvider, but it doesn't call isExpired method. Also bindShared is renamed to singleton in 5.5 – Aleksandrs Dec 13 '17 at 12:05
  • 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.php – tripper54 Feb 07 '19 at 20:25
  • Latest version of laravel support this by default, check below: https://stackoverflow.com/a/71790040/8485567 – Bernard Wiesner Apr 07 '22 at 23:31
13

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

CACHE_EXPIRE=-1
Richard
  • 5,584
  • 1
  • 19
  • 22
12

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

Bernard Wiesner
  • 961
  • 6
  • 14
6

Solution

open php.ini

opcache.revalidate_freq=0
opcache.fast_shutdown=0

change to this. restart apache.

veyselsahin
  • 368
  • 4
  • 13
  • 7
    How does this relate to laravel? – Benubird Jun 15 '15 at 09:03
  • 10
    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. – Benubird Jun 16 '15 at 13:22
4

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

Joshua Oluikpe
  • 513
  • 6
  • 9
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
suspectus
  • 16,548
  • 8
  • 49
  • 57
0

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.

Dale K
  • 25,246
  • 15
  • 42
  • 71
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";
});
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....

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

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

MUHINDO
  • 788
  • 6
  • 10
-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');
}
zanderwar
  • 3,440
  • 3
  • 28
  • 46
-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"
lczapski
  • 4,026
  • 3
  • 16
  • 32
treanorv
  • 11
  • 1
  • 2
-3

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,
    
Martin
  • 1,986
  • 15
  • 32
Tonio
  • 1
  • 1
    Does not work. This may be due to laravel version - I am using 4.1 currently; was this changed in a later version? – Benubird Dec 30 '14 at 09:30