Setting ETags in Laravel 5

Published by 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;
3use Closure;
5class ETagMiddleware {
7 /**
8 * Implement Etag support
9 *
10 * @param \Illuminate\Http\Request $request
11 * @param \Closure $next
12 * @return mixed
13 */
14 public function handle($request, Closure $next)
15 {
16 // Get response
17 $response = $next($request);
19 // If this was a GET request...
20 if ($request->isMethod('get')) {
21 // Generate Etag
22 $etag = md5($response->getContent());
23 $requestEtag = str_replace('"', '', $request->getETags());
25 // Check to see if Etag has changed
26 if($requestEtag && $requestEtag[0] == $etag) {
27 $response->setNotModified();
28 }
30 // Set Etag
31 $response->setEtag($etag);
32 }
34 // Send response
35 return $response;
36 }

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:


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.