Returning Method Not Allowed
When the path matches, but the HTTP method does not, your application should
405 Method Not Allowed status in response.
To enable that functionality, we provide
Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware via the
This middleware triggers when the following conditions occur:
- The request composes a
RouteResultattribute (i.e., routing middleware has completed), AND
- the route result indicates a routing failure due to HTTP method used (i.e.,
When these conditions occur, the middleware will generate a response:
- with a
405 Method Not Allowedstatus, AND
Allowheader indicating the HTTP methods allowed.
Pipe the middleware after the routing middleware; if using one or more of the implicit methods middleware, this middleware must be piped after them, as it will respond for any HTTP method!
$app->pipe(RouteMiddleware::class); $app->pipe(ImplicitHeadMiddleware::class); $app->pipe(ImplicitOptionsMiddleware::class); $app->pipe(MethodNotAllowedMiddleware::class); // ... $app->pipe(DispatchMiddleware::class);
(Note: if you used the Expressive skeleton, this middleware is likely already in your pipeline.)