slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

[HowTo] Call App::dispatchRouterAndPrepareRoute() inside an app middleware ? #1905

Closed wollanup closed 7 years ago

wollanup commented 8 years ago

Here is the process I would follow :

$app->add('authorization');
$app->add('authentication');
... another middleware to remove content extension from URI(.json, .xml...) ...
$app->add(Middleware::formatNegotiator());
$app->add(Middleware::trailingSlash());

My last Middleware "authorization" is ACL service based on routes Names, so it requires 'determineRouteBeforeAppMiddleware' => true, in config.

But if I determine routes before a call to my middlewares, it can't work. e.g : URI : GET /api/resource/list.json/ Route : $app->get('/resource/list') URI doesn't match my route pattern.

The workaround I currently use is to execute code inside Slim\App::dispatchRouterAndPrepareRoute() protected method as a last middleware before Authentification, but maybe there could be a cleaner way to do this without duplication of code or Slim\App class extend. Any thoughts about this ?

akrabat commented 8 years ago

Extending App is the way to do this at the moment. I expect that in 4.0 we'll convert the route determination logic to middleware so that you can put middleware before and after it.

wollanup commented 8 years ago

OK, thank you for your fast answer, I'll wait for 4.0

designermonkey commented 7 years ago

@akrabat, anywhere for this to be logged as a requested feature, so it doesn't get missed?

akrabat commented 7 years ago

This is logged in https://github.com/slimphp/Slim/issues/1686 as:

Remove determineRouteBeforeDispatch and make routing a middleware that can be placed where you want it to be. If you don't add it yourself, we would add it immediately before dispatch.