How to disable Laravel Caching

Learn how to disable caching in Laravel with an example. Easily improve performance & reduce load times for your web applications.

Caching is a critical aspect of web development that helps to improve the performance of your Laravel applications. By caching data and files, you can significantly reduce the amount of time it takes for a page to load. Laravel provides support for caching out of the box, and it can be easily enabled or disabled.

Disabling Caching in Laravel

To disable caching in Laravel, you need to edit the config/cache.php file. Inside this file you will find the following option:

'default' => env('CACHE_DRIVER', 'file'),

By default, this option is set to file, which means that Laravel will use the file caching driver. To disable caching, change the value of this option to null:

'default' => env('CACHE_DRIVER', null),

This will disable caching for your entire application. If you want to disable caching for a specific part of your application, you can do so by editing the app/Http/Kernel.php file. In this file, you will find the following lines:


protected $middlewareGroups = [
    'web' => [
        AppHttpMiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
        IlluminateSessionMiddlewareStartSession::class,
        // ...
    ],
];

The StartSession middleware is responsible for starting the session and caching requests. To disable caching, you need to comment out this middleware:


protected $middlewareGroups = [
    'web' => [
        AppHttpMiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
        // IlluminateSessionMiddlewareStartSession::class,
        // ...
    ],
];

This will disable caching for the specified part of your application. You can also disable caching for a specific route by using the withoutMiddleware() method:


Route::get('/my-route', function () {
    //
})->withoutMiddleware(StartSession::class);

This will disable caching for the specified route. You can also disable caching for specific views by using the withoutMiddleware() method:


View::make('my-view')->withoutMiddleware(StartSession::class);

This will disable caching for the specified view. By following these steps, you can easily disable caching in your Laravel applications.

Answers (0)