A simple caching filter that caches Laravel's Response objects






The Monkeys

Laravel Blade Cache Filter

A simple caching filter that caches Laravel's Response objects.


To get the latest version of the filter simply require it in your composer.json file by running:

composer require themonkeys/blade-cache-filter:dev-master --no-update
composer update themonkeys/blade-cache-filter

Once the filter is installed you need to register the service provider with the application. Open up app/config/app.php and find the providers key.

'providers' => array(

Now add the filter to the bottom of your app/filters.php file:

if (Config::get('blade-cache-filter::bladeCacheExpiry') > 0) {
    Route::filter('cache', 'BladeCacheFilter');

The filter caches your responses using the standard Laravel cache subsystem, so you might want to configure that too by following the Laravel documentation. Laravel comes configured with sensible defaults that work, so you can leave that part until later if you prefer.


After following the instructions above, the filter is installed but not attached to any routes. To add caching to a route, you need to add the 'cache' filter both 'before' and 'after' the route in your app/routes.php file:

Route::get('/url', array('before' => 'cache', 'after' => 'cache', 'uses' => '[email protected]'));

To add caching to a collection of routes, use a route group:

Route::group(array('before' => 'cache', 'after' => 'cache'), function() {
    Route::get('/url1', '[email protected]');
    Route::get('/url2', '[email protected]');


To configure the package, you can use the following command to copy the configuration file to app/config/packages/themonkeys/blade-cache-filter.

php artisan config:publish themonkeys/blade-cache-filter

Or you can just create a new file in that folder and only override the settings you need.

The settings themselves are documented inside config.php. The default configuration is for the filter to do nothing, so you'll need to at least set the 'bladeCacheExpiry' property to a positive number of minutes in the environments where you want caching to be enabled.


In lieu of a formal styleguide, take care to maintain the existing coding style.


MIT License (c) The Monkeys