PHPackages                             jshannon63/laravel-psr15-middleware - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. jshannon63/laravel-psr15-middleware

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

jshannon63/laravel-psr15-middleware
===================================

Allows the Use of PSR-15 Compliant Middleware in Laravel

v2.0(5y ago)134.2k5[1 issues](https://github.com/jshannon63/laravel-psr15-middleware/issues)MITPHPPHP ^7.3CI failing

Since Nov 28Pushed 5y ago2 watchersCompare

[ Source](https://github.com/jshannon63/laravel-psr15-middleware)[ Packagist](https://packagist.org/packages/jshannon63/laravel-psr15-middleware)[ RSS](/packages/jshannon63-laravel-psr15-middleware/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (10)Dependencies (7)Versions (15)Used By (0)

Use PSR-15 compliant middleware in Laravel
==========================================

[](#use-psr-15-compliant-middleware-in-laravel)

#### What it does and why:

[](#what-it-does-and-why)

PHP-FIG standards related to the HTTP Message Interface (PSR-7) have been in place for some time now. The standard for HTTP Handlers (PSR-15) is approved as of Jan 22, 2018.

Laravel already provides a pathway for obtaining PSR-7 request objects from route closures or controller methods. Laravel also allows returning PSR-7 response objects from a route or controller. However, having a new PSR related to middleware doesn't necessarily mean that Laravel needs to implement a compliant middleware stack... nor should it. Using a bridge (like this library) is a perfectly acceptable way to adopt PSR-15 capabilities within Laravel without completely changing the underlying framework.

Middleware is a simple thing and for the most part, middleware should be thin and are easily written. In fact, many are nothing more than single line libraries that could be just as easily created as imported. However, there is some value in re-usable web components that can be shared between applications/frameworks. Many PSR-15 middleware components already exist in the PHP community, some of which can provide some value. Having them available to use in Laravel could be a benefit.

For what benefit may be gained out of re-usable middleware logic, however small... this library was created.

The laravel-psr15-middleware library (a.k.a. Psr15Middleware) is a Laravel compatible middleware that creates a bridge between PSR-7/PSR-15 interfaces and Laravel's middleware stack and Foundation HTTP message objects.

Once installed, you will be able to run compliant PSR-15 middleware in Laravel using this package's integration in the existing Laravel middleware stack.

#### PSR implementation reasoning. TL;DR

[](#psr-implementation-reasoning-tldr)

This library fully implements the PSR-7 (psr/http-message) message object interfaces. The interface is realized through Zend Diactoros concrete implementations of both the Request and Response objects. It also fully implements the newly approved PSR-15 (psr/http-server-middleware) middleware and (psr/http-server-handler) request handler interfaces. This library uses the Symfony PSR-7 Bridge to make the conversions in both directions between Foundation and PSR-7 message objects.

Installation
------------

[](#installation)

Within your Laravel project folder, install this package using composer. If you are using Laravel 5.5 or later, service provider registration will happen automatically.

```
composer require jshannon63/laravel-psr15-middleware
```

Then use artisan to publish the package's configuration assets

```
php artisan vendor:publish

Which provider or tag's files would you like to publish?:
  [0] Publish files from all providers and tags listed below
  [1] Provider: Fideloper\Proxy\TrustedProxyServiceProvider
  [2] Provider: Illuminate\Mail\MailServiceProvider
  [3] Provider: Illuminate\Notifications\NotificationServiceProvider
  [4] Provider: Illuminate\Pagination\PaginationServiceProvider
  [5] Provider: Jshannon63\Psr15Middleware\Psr15MiddlewareServiceProvider
  [6] Tag: laravel-mail
  [7] Tag: laravel-notifications
  [8] Tag: laravel-pagination
 >

 choose the Psr15MiddlewareServiceProvider
```

That's it! Now you can configure and run your PSR-15 middleware. The default configuration in `/config/psr15middleware.php` comes with the exampleMiddleware enabled for demonstration purposes. You will need to disable all of the examples and add your own middleware classes as described below.

Usage
-----

[](#usage)

#### Add your PSR-15 compliant middlewares to the /config/psr15middleware.php configuration file.

[](#add-your-psr-15-compliant-middlewares-to-the-configpsr15middlewarephp-configuration-file)

1. It is NOT necessary to declare PSR-15 middleware in the `app/Http/Middleware/Kernel.php` file as is normally done. Psr15Middleware will automatically register itself and its middlewares by pushing them onto the Laravel middleware stack.
2. Config entries are arrays and can can contain classnames, callables or objects as shown in the example below. Each entry has two additional parameters which follow the middleware declaration:
    - "prepend" or "append" will determine if your midleware will be placed at the head or tail of the middleware stack.
    - "before", "after" and "terminable" specify the type of middleware. Before middlewares run before the application acts on the request. After middlewares run after the request has been acted on by the application, but before the response has been sent to the browser. Terminable middlewares run after the browser has received the response and are generally used for housekeeping task which require access to the request and/or response objects.
3. Additional sections for aliases ($routeMiddleware) and groups ($middlewareGroups) which closely adhere to the special route middleware groups within the `app\Http\Middleware\Kernel.php` file.
4. You can add new groups if you like (i.e., custom as shown).
5. Constructor arguments can be passed for middlewares declared as callables or objects within the configuration. All PSR-15 middleware constructors will be treated as variadic functions and therefore will be able to accept any number of arguments to their constructor. Note: These constructor arguments can also be passed as Laravel middleware route parameters. See the Laravel documentation for more on this feature.

```
