Download the PHP package phpwatch/laravel-fast404 without Composer
On this page you can find all versions of the php package phpwatch/laravel-fast404. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phpwatch/laravel-fast404
More information about phpwatch/laravel-fast404
Files in phpwatch/laravel-fast404
Package laravel-fast404
Short Description A Laravel middleware to quickly terminate Page-Not-Found pages that do not require a full HTML page
License MIT
Homepage https://github.com/PHPWatch/Laravel-Fast404
Informations about the package laravel-fast404
Laravel Fast 404
Laravel Fast 404 is a Laravel package that adds a global middleware to your Laravel application to immediately terminate HTTP(s) requests for non-HTML 404 requests. This prevents unnecessary database connections and your application from fully bootstrapping to serve an HTML page that nobody will see.
This is done by inspecting every incoming HTTP request's "Accept" header and the URI path. If the URI path ends with a known static file extension (such as .jpg
, .png
, .woff
, etc), and the Accept
header does not mention text/html
(which is the case when browsers request images, web fonts, JS files, CSS files, etc), the request is immediately terminated by this middleware.
Requirements
- Laravel 5.5+, 6, or 7
- PHP 7.4
Installation
Upon installation, Laravel will automatically register the Service Provider bundled with this package, which will in turn register middleware and the Fast404Middleware
service automatically.
Configuration
You can configure the message, the regular expression used to match URI patterns (e.g. a list of file extensions), and optionally an exclusion regular expression.
From configuration files
Update your config/app.php
file, and add the following configuration to the existing array:
All configuration values must be strings.
Configuration | Default Value | Description |
---|---|---|
message |
Not Found |
The message to be shown when this package terminates a request. It might contain HTML. Try to keep the message short. |
regex |
/\.(?:js\|css\...woff2)$/i |
A full regular expression to match against the request URI (without base-URI and URL parameters). If matched, this package will attempt to terminate the request. The default value will be used if null is passed. Make sure to include expression delimiters and flags if necessary. It is recommended to keep the default value. |
exclude_regex |
null |
An optional regular expression to match, and if matched, this package will not terminate the request even if the exclude expression matched positive. This can be used to declare exclusion patterns if your Laravel application generates images on-the-fly, provides dynamic .js files, etc. |
File type extensions
The default regular expression is:
This creates a non-capturing group of file types separated by the pipe (|
) symbol above.
By updating the service provider (advanced)
This package bundles a Service Provider that conveniently enables middleware. You can turn this feature off if you wish to configure the middleware to your liking.
Step 1: Remove the automatic provider discovery for this library
In your root composer.json
file, add/merge configuration directives:
Step 2: Add the middleware
In your application App/Http/Kernel.php
file, prepend the Middleware provided by this package.
Make sure to add \PHPWatch\LaravelFast404\Fast404Middleware::class,
to the top because middlewares are run in the order they are declared.
Optional step 3: Register a service provider for further customizations
If you would like to configure the middleware to change the message, file extensions, or the exclusion pattern, you will need to register it in the Service Container.
To do so, you can either create a new service provider, or update an existing one to declare how the \PHPWatch\LaravelFast404\Fast404Middleware
class is instantiated.
Contributions
Contributions are welcome! Please feel free to send a PR or open an issue. Please note that this Laravel package is in the same line as phpwatch/fast404
and phpwatch/wordpress-fast404
packages, and the extensions list updates will be made to all packages in a framework-agnostic way.