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

Grouping existing routes #243

Closed delboy1978uk closed 5 years ago

delboy1978uk commented 5 years ago

Imagine you have some endpoints like /blahin some separate modules/packages, but want to be able to dynamically create a /{locale} group based on an internationalisation module being enabled.

As far as I can see there are a couple of approaches to this, at first I was looking at this approach by mwop https://docs.zendframework.com/zend-expressive/v3/cookbook/setting-locale-without-routing-parameter/ , which has a middleware which strips the locale from the url before returning an altered request with the path now minus the locale. But when I use that approach the router always trys to match the route first, and 404's.

Of course I realise that the router can add groups, so the next approach was to somehow get all of the existing routes and move them inside of that group, however despite trying a few things I can't figure out how to do that! I need a way to get all existing defined routes and shift them inside the group. I tried setParentGroup($group) on routes too, but with no luck.

So I returned to the first approach, only this time I extended ApplicationStrategy, and made the middleware the not found decorator. It all looked like it was going to work, it removed the locale part of the uri, but then when it went to handle the request, it called shiftMiddleware(), the array was empty (I guess because the exception middleware is always the last in the stack? and so i got Uncaught TypeError: Return value of League\Route\Dispatcher::shiftMiddleware() must implement interface Psr\Http\Server\MiddlewareInterface.

I assume then that there is no way to run any other middleware to prepare the request before the routes attempt to get matched?