I have a route group prefixed with /api/v1 and some routes defined inside. I also have the strategy set for the route group to the bundled JsonStrategy.
Lets say the routes defined inside the /api/v1 route group are a GET route on / and a GET route on /welcome. The routes work fine, but if I try to call a route such as /api/v1/test, it will throw a NotFoundException, which is expected as the route is undefined, however the exception is not caught when the route is dispatched. I was under the impression that by setting a JsonStrategy on a route (or route group), exceptions would be caught and a json response would be returned.
I thought it might have been where those routes were undefined, so I tried it by setting the strategy on an individual route, and then throwing a NotFoundException from within the controller function, and it will fail with an uncaught exception. Here is some example code to illustrate my problem:
-- file: index.php
<?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
require '../vendor/autoload.php';
$container = new League\Container\Container;
$container->share('response', Zend\Diactoros\Response::class);
$container->share('request', function () {
return Zend\Diactoros\ServerRequestFactory::fromGlobals(
$_SERVER, $_GET, $_POST, $_COOKIE, $_FILES
);
});
$container->share('emitter', Zend\Diactoros\Response\SapiEmitter::class);
$route = new League\Route\RouteCollection($container);
$route->map('GET', '/', function (ServerRequestInterface $request, ResponseInterface $response) {
$response->getBody()->write(json_encode(['message' => 'hey']));
throw new \League\Route\Http\Exception\NotFoundException();
return $response;
})->setStrategy(new \League\Route\Strategy\JsonStrategy());
$response = $route->dispatch($container->get('request'), $container->get('response'));
$container->get('emitter')->emit($response);
I have also tried setting a route group and setting the strategy for the group:
-- file: index.php
<?php
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
require '../vendor/autoload.php';
$container = new League\Container\Container;
$container->share('response', Zend\Diactoros\Response::class);
$container->share('request', function () {
return Zend\Diactoros\ServerRequestFactory::fromGlobals(
$_SERVER, $_GET, $_POST, $_COOKIE, $_FILES
);
});
$container->share('emitter', Zend\Diactoros\Response\SapiEmitter::class);
$route = new League\Route\RouteCollection($container);
$route->group('/api/v1', function($route) {
$route->map('GET', '/', function (ServerRequestInterface $request, ResponseInterface $response) {
$response->getBody()->write(json_encode(['message' => 'hey']));
return $response;
});
})->setStrategy(new \League\Route\Strategy\JsonStrategy());
$response = $route->dispatch($container->get('request'), $container->get('response'));
$container->get('emitter')->emit($response);
It seems after looking in the dispatch function for the RouteCollection, it catches the exception, but I added some debugging to the exception handling and $this->getStrategy() is returning an instance of ApplicationStrategy would would just rethrow the exception. Perhaps I am missing something with setting up the strategies for the routes.
Any insight would be greatly appreciated.