PHPackages                             phpwatch/fast404 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. phpwatch/fast404

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phpwatch/fast404
================

A PSR-7 middleware to return 404 errors fast for requests that are not worth returning a fancy 404 error.

v2.0.2(2y ago)33.2k1MITPHPPHP ^8.3CI failing

Since May 1Pushed 1y ago1 watchersCompare

[ Source](https://github.com/PHPWatch/Fast404)[ Packagist](https://packagist.org/packages/phpwatch/fast404)[ RSS](/packages/phpwatch-fast404/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (3)Versions (10)Used By (0)

Fast 404
========

[](#fast-404)

Quickly terminate an HTTP request with a `404 Not Found` response for static resources.

[![Latest Stable Version](https://camo.githubusercontent.com/48ea2ea772be7e90e3c115e198c1835324f9c119f8fc2f62031345b8f6e77beb/68747470733a2f2f706f7365722e707567782e6f72672f70687077617463682f666173743430342f76)](https://packagist.org/packages/phpwatch/fast404) [![CI](https://github.com/PHPWatch/Fast404/workflows/CI/badge.svg?branch=master)](https://github.com/PHPWatch/Fast404/workflows/CI/badge.svg?branch=master) [![Total Downloads](https://camo.githubusercontent.com/5cdb5a8a7c67a007679cfca02675f524bbdf90fc7d4998bb0a8c8fc4ff2bc401/68747470733a2f2f706f7365722e707567782e6f72672f70687077617463682f666173743430342f646f776e6c6f616473)](https://packagist.org/packages/phpwatch/fast404) [![License](https://camo.githubusercontent.com/bae9a023ff5b6370a5f86c8984d6f4851eaf3cab89c1708b6a466281d448b03e/68747470733a2f2f706f7365722e707567782e6f72672f70687077617463682f666173743430342f6c6963656e7365)](https://github/phpwatch/fast404)

`phpwatch/fast404` is a library/middleware that you can to quickly terminate an HTTP request with a `404 Not Found` response.

The use case is a framework that handles all HTTP requests via a router, and returning a quick 404 message to static resources such as `.jpg` or `.png`. This prevents the framework from initializing rendering engines, database connections, etc to serve these types of requests.

It is common that PHP frameworks use a router to handle incoming HTTP requests. Web server forwards all requests to the PHP framework (often to the `index.php` file). This has the side-effect of static resources such as `.jpg` or `.png` requests being routed to the PHP framework as well.

Frameworks can generate a nice 404 Not Found error page, it is a waste of resources to generate nice error pages for images, videos, and other embeded content that are not the main URL the user accessed.

This package comes with configurable but sensible defaults, and when added as a middleware, it is executed early in the request, and short-circuits the rest of the bootstrap process and returns a quick and dirty "Not Found" page with the proper HTTP header. This can reduce the overhead by not having to connect to the database or fire up a rendering engine.

Note that the rest of the execution will be terminated with a PHP `die()` call. If you want to log the error messages via the framework events or other middleware, this package is not for you.

### Installation

[](#installation)

```
composer require phpwatch/fast404
```

### Usage

[](#usage)

You need to execute the provided middleware within your framework. Here is an example for Slim v3:

**1. Add `Fast404Middleware` class to the Container**

```
