Looking to hire Laravel developers? Try LaraJobs

laravel-etag maintained by tinigin

Description
Setting the ETag header and 304 Not Modified response code if the page hasn't changed since the last visit
Last update
2026/03/31 14:50 (dev-main)
License
Downloads
98

Comments
comments powered by Disqus

ETag / 304 Not Modified Handler for Laravel

Easily setting the ETag header and 304 Not Modified response code.

Requirements

  • PHP 8.3+
  • Laravel 11.x / 12.x / 13.x

Installation

You can install the package via composer:

composer require tinigin/laravel-etag

Optionally, you can publish the config file with:

php artisan vendor:publish --tag="etag-config"

Usage

The setup is very simple and consists of two steps:

Registering middleware

// in bootstrap/app.php

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \Tinigin\ETag\Middleware\ETagHandling::class,
    ]);
})

Set Last Update Date in your Controller

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use ETag;
 
class PostController extends Controller
{
    public function show($id)
    {
        $post = \App\Models\Post::findOrFail($id);

        ETag::set($post->getKey());
        
        return view('posts.show', ['post' => $post]);
    }
}

It's all. Now you can check the headers.

How to check headers

You can check headers in the browser console under the Network tab (make sure Disable Cache is off)

Testing

composer test:all

or

composer test:phpunit
composer test:phpstan
composer test:phpcsf

or see https://github.com/tinigin/laravel-etag/actions/workflows/tests.yml

Feedback

If you have any feedback, comments or suggestions, please feel free to open an issue within this repository.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.