Benubird
Benubird

Reputation: 19487

How do I disable Laravel view cache?

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?

Upvotes: 36

Views: 94853

Answers (14)

Matteus Barbosa
Matteus Barbosa

Reputation: 2725

cache attribute controls whether blades are going to be cached in /storage/framework/views or not. Create config/view.php file:

<?php

return [

 /* Caching disabled when debug is enabled.  */
 'cache' => (bool) !env('APP_DEBUG', true),

    /*
    |--------------------------------------------------------------------------
    | View Storage Paths
    |--------------------------------------------------------------------------
    |
    | Most templating systems load templates from disk. Here you may specify
    | an array of paths that should be checked for your views. Of course
    | the usual Laravel view path has already been registered for you.
    |
    */

    'paths' => [
        resource_path('views'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Compiled View Path
    |--------------------------------------------------------------------------
    |
    | This option determines where all the compiled Blade templates will be
    | stored for your application. Typically, this is within the storage
    | directory. However, as usual, you are free to change this value.
    |
    */

    'compiled' => env(
        'VIEW_COMPILED_PATH',
        realpath(storage_path('framework/views'))
    ),


];

Upvotes: 0

Bernard Wiesner
Bernard Wiesner

Reputation: 1415

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

Upvotes: 14

MUHINDO
MUHINDO

Reputation: 1213

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

Upvotes: 0

Acute Club
Acute Club

Reputation: 1

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....

Upvotes: 0

Parsa Samandizadeh
Parsa Samandizadeh

Reputation: 594

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

Upvotes: 0

treanorv
treanorv

Reputation: 11

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"

Upvotes: -1

suspectus
suspectus

Reputation: 17268

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

Upvotes: 3

Richard
Richard

Reputation: 5880

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

CACHE_EXPIRE=-1

Upvotes: 16

Joshua Oluikpe
Joshua Oluikpe

Reputation: 600

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

Upvotes: 4

Prem Sarojanand
Prem Sarojanand

Reputation: 186

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.

Upvotes: -1

zanderwar
zanderwar

Reputation: 3730

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

Upvotes: -1

veyselsahin
veyselsahin

Reputation: 388

Solution

open php.ini

opcache.revalidate_freq=0
opcache.fast_shutdown=0

change to this. restart apache.

Upvotes: 6

Tonio
Tonio

Reputation: 11

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,
    

Upvotes: -2

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

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

];

Upvotes: 26

Related Questions