Setting ETags in Laravel 5
Published by Matthew Daly at 14th June 2015 8:29 pm
Although I'd prefer to use Python or Node.js, there are some times when circumstances dictate that I need to use PHP for a project at work. In the past, I used CodeIgniter, but that was through nothing more than inertia. For some time I'd been planning to switch to Laravel, largely because of the baked-in PHPUnit support, but events conspired against me - one big project that came along had a lot in common with an earlier one, so I forked it rather than starting over.
Recently I built a REST API for a mobile app, and I decided to use that to try out Laravel (if it had been available at the time, I'd have gone for Lumen instead). I was very pleased with the results - I was able to quickly put together the back end I wanted, with good test coverage, and the tinker
command in particular was useful in debugging. The end result is fast and efficient, with query caching in place using Memcached to improve response times.
I also implemented a simple middleware to add ETags to HTTP responses and compare them on incoming requests, returning a 304 Not Modified
status code if they are the same, which is given below:
1<?php namespace App\Http\Middleware;23use Closure;45class ETagMiddleware {67 /**8 * Implement Etag support9 *10 * @param \Illuminate\Http\Request $request11 * @param \Closure $next12 * @return mixed13 */14 public function handle($request, Closure $next)15 {16 // Get response17 $response = $next($request);1819 // If this was a GET request...20 if ($request->isMethod('get')) {21 // Generate Etag22 $etag = md5($response->getContent());23 $requestEtag = str_replace('"', '', $request->getETags());2425 // Check to see if Etag has changed26 if($requestEtag && $requestEtag[0] == $etag) {27 $response->setNotModified();28 }2930 // Set Etag31 $response->setEtag($etag);32 }3334 // Send response35 return $response;36 }3738}
This is based on a solution for Laravel 4 by Nick Verwymeren, but implemented as Laravel 5 middleware, not a Laravel 4 filter. To use this with Laravel 5, save this as app/Http/Middleware/ETagMiddleware.php
. Then add this to the $middleware
array in app/Http/Kernel.php
:
'App\Http\Middleware\ETagMiddleware',
It's quite simple to write this kind of middleware with Laravel, and using something like this is a no-brainer for most web apps considering the bandwidth it will likely save your users.