zendframework / zend-expressive-router

Router subcomponent for Expressive
BSD 3-Clause "New" or "Revised" License
28 stars 13 forks source link

Refactor routing and dispatch logic #48

Closed weierophinney closed 6 years ago

weierophinney commented 6 years ago

This patch accomplishes several things.

First, it extracts the logic for producing a 405 response to new middleware, Zend\Expressive\Router\MethodNotAllowedMiddleware. This middleware checks for a route result that is due to method failure, and, in such a case, uses the composed response prototype. This change will allow users to substitute their own MethodNotAllowedMiddleware, allowing for templated solutions, or to omit it entirely, allowing fallback to the 404 handler.

Second, the RouteMiddleware was modified as follows:

These changes mean the middleware does only what it says (routing).

Third, the Route class was modified to implement the PSR-15 MiddlewareInterface. When processed, it proxies to the composed middleware instance.

Fourth, the RouteResult was modified to implement the PSR-15 MiddlewareInterface. When processed, if it is a route failure, it delegates to the provided handler. If it is a successful route match, it proxies to the matched Route instance. The getMatchedMiddleware() method was removed, as it's essentially unnecessary; processing the route result accomplishes the same thing.

Fifth, the DispatchMiddleware was modified to process the RouteResult instance pulled from the request, instead of retrieving its composed middleware to process.