Add ability to disable controller middleware #53350
Closed
+151
−16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds the ability to disable the collection of controller middleware.
The main reason behind this is to prevent the controller's constructor from being run before the route is being processed. Primarily for dependency-injection purposes.
This implementation is non-invasive and comes in two parts.
Middleware Config/Builder
When building the application controller, middleware can be disabled for the entire application.
It can also be re-enabled.
Note
As a side effect of this, the default HTTP Kernel implementation now has a
Kernel::getRouter()
method that serves as a getter forKernel::$router
.Router
You can also disable or enable controller middleware directly on the router.
And re-enable.
Side Effect/Possible Bug-fix
Some tests failed the first time around, and I discovered it's because the
OPTIONS
routes that are automatically created are created without access to the router. This means thatRoute::$router
isnull
, which doesn't seem to be allowed by its docblock.To fix this, I moved
CompiledRouteCollection::setRouter()
toAbstractRouteCollection
, and had theRouteCollection
class call$this->router->newRoute()
instead of manually creating a new instance ofRoute
.I suspect this was a side effect bug that hadn't been caught yet, as it's quite obscure,