CACHING VIEWS
We have heard about caching queries , caching views but what if we want to cache a part of our view , a section or a div . Let’s create a view we will use a template for our view to view click here.
content.blade.php
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-12">
<h1>Alice in Wonderland, part dos</h1>
<p>'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they
both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to
the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
'Yes, we went to school in the sea, though you mayn't believe it'
'I never said I didn't!' interrupted Alice.
'You did,' said the Mock Turtle.'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they
both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to
the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
'Yes, we went to school in the sea, though you mayn't believe it'
'I never said I didn't!' interrupted Alice.
'You did,' said the Mock Turtle.'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they
both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to
the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
'Yes, we went to school in the sea, though you mayn't believe it'
'I never said I didn't!' interrupted Alice.
'You did,' said the Mock Turtle.'You ought to be ashamed of yourself for asking such a simple question,' added the Gryphon; and then they
both sat silent and looked at poor Alice, who felt ready to sink into the earth. At last the Gryphon said to
the Mock Turtle, 'Drive on, old fellow! Don't be all day about it!' and he went on in these words:
'Yes, we went to school in the sea, though you mayn't believe it'
'I never said I didn't!' interrupted Alice.
'You did,' said the Mock Turtle.</p>
<div>
<span class="badge">Posted 2012-08-02 20:47:04</span>
<div class="pull-right"><span class="label label-default">alice</span> <span class="label label-primary">story</span>
<span class="label label-success">blog</span> <span class="label label-info">personal</span> <span
class="label label-warning">Warning</span>
<span class="label label-danger">Danger</span></div>
</div>
</div>
</div>
</body>
</html>
Above code shows static content lets make it dynamic. I have to create required migrations and seeding and inserted that data into table lets fetch and display that
routes.php
Route::get('/content/{id}', function($id){
$content=Content::find($id);
$content->tags=explode(',',$content->tags);
return View::make('content',compact('content'));
});
Content.php
/**
* Content
*
*/
class Content extends Eloquent{
protected $table = 'content';
}
After changing our static content to dynamic it will look like below
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-12">
<h1>{{$content->header}}</h1>
<p>{{$content->content}}</p>
<div>
<span class="badge">Posted {{$content->created_at}}</span>
@foreach($content->tags as $tag)
<div class="pull-right">
<span class="label label-warning">{{$tag}}</span>
</div>
@endforeach
</div>
</div>
</div>
</body>
</html>
Let’s create our Cache class now. Create a new folder inside your app and name it Cache
Cache/PageCache.php
namespace Cache;
use URL;
use Cache;
use Closure;
class PageCache
{
public static function cache($key, $expires = 1, Closure $closure)
{
$key = $key . URL::full();
if ( Cache::has( $key ) )
{
$content = Cache::get( $key );
}
else
{
ob_start();
$closure();
$content = ob_get_contents();
ob_end_clean();
Cache::put( $key, $content, $expires );
}
echo $content;
}
}
I don’t like calling Cache\PageCache in my view so let’s add an alias to our aliases array in app.php
'PageCache' => 'Cache\PageCache'
We have to tell composer to load our namespace so add app\Cache\ to autoload class map
"app/Cache",
The last thing is to use our new class in our view don’t forget to pass variable to function used as parameters
<?php PageCache::cache('content',5,function() use ( $content ) { ?>
<div class="col-md-12">
<h1>{{$content->header}}</h1>
<p>{{$content->content}}</p>
<div>
<span class="badge">Posted {{$content->created_at}}</span>
@foreach($content->tags as $tag)
<div class="pull-right">
<span class="label label-warning">{{$tag}}</span>
</div>
@endforeach
</div>
</div>
<?php }); ?>
Now our above section of view is cached for 5 mins try changing data in DB and refresh the page it won’t change the values since our section of that view is cached. Enjoy.
No Comments
Leave a comment Cancel