kudashevs/laravel-last-modified

A Laravel middleware that handles the If-Modified-Since request and Last-Modified response headers.

v1.4.0 2025-01-21 13:23 UTC

This package is auto-updated.

Last update: 2025-02-28 09:30:54 UTC


README

Latest Version on Packagist Run Tests License MIT

This Laravel package contains a handler for the If-Modified-Since request and Last-Modified response headers.

Installation

You can install the package via composer:

composer require kudashevs/laravel-last-modified

Then, register the middleware in the app/Http/Kernel.php:

protected $middleware = [
    'web' => [
        ...
        \Kudashevs\LaravelLastModified\Middleware\LastModified::class,
    ],
];

You may also want to publish the configuration file (optional).

php artisan vendor:publish --provider="Kudashevs\LaravelLastModified\Providers\LastModifiedServiceProvider"

After deploying your application, check whether the headers are handled correctly.

How it works

First, the middleware identifies the last modification time of a given url by parsing the request. It tries to retrieve this information from possible origins in the following order (if it cannot retrieve the information, it moves forward):

  • a first model in the view data,
  • a first collection in the view data,
  • a first paginator in the view data,
  • a compiled view file,
  • a view file,
  • as a last resort, a fallback.

After the last modification time has been retrieved, the middleware sets the Last-Modified response header. Then, it handles the If-Modified-Since request header using the last modification time from the previous step.

Configuration

After publishing, the configuration settings are located in the config/last-modified.php file.

There configuration options are currently supported:

'enable'               # A boolean defines whether the middleware is enabled (default `true`).
'aggressive'           # A boolean defines whether the middleware returns a response immediately (default is `false`).
'fallback'             # An integer the fallback timestamp for the Last-Modifier header.
'origins'              # An array with a list of origins of the last modification time.
'exclude'              # An array with a list of paths to be excluded from processing.

, for more information please see the configuration file.

Testing

composer test

References

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Note: Please make sure to update tests as appropriate.

License

The MIT License (MIT). Please see the License file for more information.