thephpleague / route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
http://route.thephpleague.com
MIT License
651 stars 126 forks source link

Allow middleware to run before routing occurs #269

Closed delboy1978uk closed 4 years ago

delboy1978uk commented 4 years ago

I left a comment on this closed issue https://github.com/thephpleague/route/issues/261

If we wish to manipulate a request using middleware before any routing occurs, then this isn't currently possible.

To remedy this, we could either have a pre-routing middleware stack, or you could make the Router implement RequestHandlerInterface, since this method here

public function dispatch(ServerRequestInterface $request): ResponseInterface

is essentially the same as the handle method

public function handle(ServerRequestInterface $request): ResponseInterface

which would allow us to pass the router into another stack.

Personally I prefer the first idea, however it would do no harm whatsoever to also implement the interface, the dispatch method could be @deprecated and call the handle method instead.

I have sent a pull request for the RequestHandlerInterface approach. https://github.com/thephpleague/route/pull/271

Your thoughts?