Open ericktucto opened 1 year ago
You need to implement a custom strategy (you can extend the existing ApplicationStrategy
or JsonStrategy
), and implement or override the getThrowableHandler()
method in your custom strategy.
This method should return an implementation of MiddlewareInterface
that forwards the given ServerRequestInterface
to the next RequestHandlerInterface
, wrapped it in a try/catch so that it can catch any Throwables and convert them to responses.
Take a look at the implementation in JsonStrategy
for an example: https://github.com/thephpleague/route/blob/5.x/src/Strategy/JsonStrategy.php#L64-L98
You can then use your custom strategy on a route group, a single route, or the whole router depending on your needs. In your case I think you'll need to create 2 custom strategies, one for HTML pages and one for JSON endpoints, and assign them to the right routes/groups like you did with the default ApplicationStrategy
and JsonStrategy
in your example.
Disclaimer: I'm not the author/maintainer of this package.
I like send a Response if Method Not Found o route Not Found
example:
or