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

Lazy resolving of middleware #1604

Closed chriseskow closed 8 years ago

chriseskow commented 8 years ago

I was playing around with Slim 3 and noticed that for group and route middleware, Slim will resolve the callable immediately when the middleware is added, instead of only when it is actually needed and called. This means if a particular route or group has a middleware defined by a DI container key which is potentially expensive to instantiate (e.g. because a database connection is injected into the constructor), it will be created on every request, not just requests that utilize that middleware.

Any chance that callable resolving for route- and group-level middleware could be deferred to when the middleware is executed, instead of when it is added? (App-level middleware shouldn't matter, since they run on every request anyway.)

geggleto commented 8 years ago

the thing is, on routes and groups the callables need to be present in order to be added to the stack. Having this would require a significant rewrite of the middleware process, but it is possible.

I would imagine this could be done for 3.1

mathmarques commented 8 years ago

@chriseskow I made a modification to fix this here: https://github.com/mathmarques/Slim/tree/lazy-resolving-middleware The change: https://github.com/mathmarques/Slim/commit/99a85f9f1fbabfde730e160898da0024e56c7bb3

I'm justing trying to figure out why Slim needs to finalize() all routes via Router->finalize(). We just need to finalize the dispatched route.

I leave the Router->finalize() because of the Tests.

akrabat commented 8 years ago

Something for 3.1.0. See https://github.com/slimphp/Slim/issues/1627

akrabat commented 8 years ago

Fixed via #1716

chriseskow commented 8 years ago

Thanks for getting this done, guys! :sparkles: