Created
July 3, 2016 21:57
-
-
Save mogetutu/2f78446728242b938c7a2d8d72e1787f to your computer and use it in GitHub Desktop.
ETag middleware laravel 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Middleware; | |
use Closure; | |
class ETagMiddleware { | |
/** | |
* Implement Etag support | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
// Get response | |
$response = $next($request); | |
// If this was a GET request... | |
if ($request->isMethod('get')) { | |
// Generate Etag | |
$etag = md5($response->getContent()); | |
$requestEtag = str_replace('"', '', $request->getETags()); | |
// Check to see if Etag has changed | |
if($requestEtag && $requestEtag[0] == $etag) { | |
$response->setNotModified(); | |
} | |
// Set Etag | |
$response->setEtag($etag); | |
} | |
// Send response | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment