Integrating PSR-15 Middleware into Laravel with the Laravel PSR-15 Bridge Adapter
This article explains how PSR-15 middleware works, why Laravel needs a bridge adapter, and provides step‑by‑step instructions for installing and configuring the Laravel PSR-15 Bridge to seamlessly integrate standard PHP middleware into Laravel applications.
In modern web applications, middleware plays an essential role in handling cross‑cutting concerns, and while Laravel includes its own middleware system, adopting the PSR‑15 standard brings greater interoperability across PHP projects.
Laravel's Need for a PSR‑15 Adapter
PSR‑15 middleware follows the PSR‑7 message standard, allowing any framework that supports PSR‑7 to run framework‑agnostic middleware. However, Laravel does not natively support PSR‑15, prompting the creation of a bridge adapter.
What Is PSR‑15 Middleware?
It is a PHP class that implements the Psr\Http\Server\MiddlewareInterface interface.
namespace Psr\Http\Server;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
interface MiddlewareInterface
{
/**
* Process an incoming server request to produce a response.
* If unable to generate a response, it may delegate to the provided request handler.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface;
}The middleware relies on PSR‑7 messages and must communicate via the RequestHandlerInterface as defined by the standard.
namespace Psr\Http\Server;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Handles a server request and produces a response.
*/
interface RequestHandlerInterface
{
/**
* Handle the request and return a response.
* May invoke other middleware to generate the response.
*/
public function handle(ServerRequestInterface $request): ResponseInterface;
}To comply with PSR‑15, you only need to create a class implementing MiddlewareInterface and ensure the next middleware is invoked via a request handler. Laravel itself does not support this out of the box.
How the Laravel PSR‑15 Bridge Works
The bridge acts as a converter, linking Laravel’s Illuminate request/response objects with the PSR‑7 format required by PSR‑15 middleware. It wraps Illuminate requests/responses into native PSR‑7 objects, enabling seamless communication between Laravel and PSR‑15 components.
It also implements RequestHandlerInterface, managing the flow of requests and responses through the middleware chain, ensuring each middleware receives the correct input and passes it onward.
Throughout the chain, the bridge continuously monitors and converts data, allowing PSR‑15 middleware to process requests and responses according to its own logic, thus achieving smooth integration.
Using the Laravel PSR‑15 Bridge Adapter
Installation is straightforward via Composer: composer require softonic/laravel-psr15-bridge After installing, you can bind a PSR‑15 middleware class to the bridge in a service provider:
$this->app->bind(\Vendor\AmazingMiddleware::class, function () {
return Psr15MiddlewareAdapter::adapt(new \Vendor\AmazingMiddleware());
});Then register the adapted middleware on a route just like any Laravel middleware:
Route::get('/my-route', function () {
// ...
})->middleware([\Vendor\AmazingMiddleware::class]);For more details and examples, refer to the README of the Laravel PSR‑15 Bridge repository.
Conclusion
The Laravel PSR‑15 Bridge Adapter provides developers with an easy way to integrate standards‑compliant, framework‑agnostic PSR‑15 middleware into Laravel applications, bridging the gap between Laravel’s middleware system and the broader PSR‑15 ecosystem. This enhances code reusability, modularity, and interoperability, offering Laravel developers a future‑proof solution aligned with evolving best practices.
Whether starting a new Laravel project or maintaining an existing one, exploring PSR‑15 middleware and the bridge adapter opens new possibilities for flexibility and maintainability, benefiting both individual developers and the wider PHP community.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
