Hi!
I’m not sure if this is the correct place to publish this. Maybe I should open an issue on github. Just let me know it.
A few days ago I asked on twitter what was the best way to customize the response injected on the RouteMiddleware. https://twitter.com/acelayaa/status/864374315248123904
However, a while later I ended up defining my own middleware factory which returns a RouteMiddleware with my custom response prototype. Something like this (this is simplified):
<?php
use Zend\Expressive\Middleware\RouteMiddleware;
class RoutingMiddlewareFactory
{
public function __invoke(ContainerInterface $container)
{
$respProto = new JsonResponse([
'error' => 'METHOD_NOT_ALLOWED',
'message' => 'Method not allowed',
], 405);
return new RouteMiddleware($container->get(RouterInterface::class), $respProto);
}
}
The problem was that it is not possible to register this factory using the Application::ROUTING_MIDDLEWARE name.
When that name is used for a middleware, the Application always instantiates a new RouteMiddleware, instead of trying to fetch a service from the container. I think the problem is in the prepareMiddleware method in MarshalMiddlewareTrait.
I haven’t checked it, but I believe something similar happens if you use the Application::DISPATCH_MIDDLEWARE name
The solution was easy, I think. I just used another name and piped that middleware instead of Application::ROUTING_MIDDLEWARE, however it feels a little bit counterintuitive.
Is this behavior intended?