[Slim 4] Get router in controller

Before (in Slim 3), to get a route from a Controller you could do:

But how do we do it now?

You need to obtain the RouteParser from the RouteCollector

$app = AppFactory::create();

$routeParser = $app->getRouteCollector()->getRouteParser();

// pathFor() has been replaced by urlFor()

Oh, yes, indeed. Thank you :slight_smile:

@l0gicgate but if I have code structure taken from github and my action looks like:

     * Action.
     * @param ServerRequestInterface $request The request
     * @param ResponseInterface $response The response
     * @return ResponseInterface The response
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
       // ... I would like to call $routeParser->urlFor(...); here

could you suggest me what should I do?

Use the route context object.

user Slim\Routing\RouteContext;

$app->get('/', function ($request, $response) {
    $routeContext = RouteContext::fromRequest($request);
    $routeParser = $routeContext->getRouteParser();
    $relativeUrl = $routeParser->relativeUrlFor('homepage');
    $url = $routeParser->urlFor('homepage');
    $fullUrl = $routeParser->fullUrlFor('homepage');

@l0gicgate thanks, this is what I was doing so far, I was just wondering if there is any other better approach.