Skip to content

Instantly share code, notes, and snippets.

@carbontwelve
Last active December 30, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbontwelve/7888347 to your computer and use it in GitHub Desktop.
Save carbontwelve/7888347 to your computer and use it in GitHub Desktop.
<?php
/*
|--------------------------------------------------------------------------
| Cache Filters
|--------------------------------------------------------------------------
|
| This filter can be attached to a route to easily add a simple caching
| layer.
|
| Use as a before and after filter. When fired after a request the cache
| will be updated if required, and when fired before a request the cache
| will be loaded if required.
|
| From: http://markvaneijk.com/caching-routes-using-filters-in-laravel-4
|
| Usage:
| Route::get('/', array
| (
| 'before' => 'cache',
| 'after' => 'cache',
| 'uses' => 'HomeController@show'
| )
| );
*/
Route::filter('cache', function($route, $request, $response = null)
{
if ( Config::get('cache.page_caching', false) === true )
{
$key = 'route-'.Str::slug(Request::url());
if(is_null($response) && Cache::has($key))
{
return Cache::get($key);
}
elseif(!is_null($response) && !Cache::has($key))
{
Cache::put($key, $response->getContent(), 30);
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment