I have two POST routes /test and /test_new. The objective is to redirect all incoming requests from /test_new to /test along with the body contents. Hence The following code aims to use a named route for redirection
$app->post('/test', function (Request $request, Response $response, $args) {
$response->getBody()->write($request->getBody()->getContents());
return $response;
})->setName('test');
$app->post('/test_new', function (Request $request, Response $response, $args) use ($app) {
$routeParser = RouteContext::fromRequest($request)->getRouteParser();
// $routeParser = $app->getRouteCollector()->getRouteParser();
return $response->withStatus(307)->withBody($request->getBody())
->withHeader('Location', $routeParser->urlFor('test'));
});
This piece of code fails with the message
/test_new - Uncaught RuntimeException: Cannot create RouteContext before routing has been completed in /home/biswa/test/test-slim/vendor/slim/slim/Slim/Routing/RouteContext.php:40
If I simply replace the $routeParser with the commented line in the second route handler and use the $app for the RouterParser it works fine. But In my actual code I have the handling logic in a class function and have no access to $app in there.
Can someone please help me to sort this out