Slim 4 get base url

In slim 3 there was a method to get the base url from the uri.
What is the right way to get the same with slim 4 ?

1 Like

The Slim 4 App class has a App::getBasePath() method


$basePath = $app->getBasePath();

But that doesn’t return the base application url. It returns the base path that is empty by default if you don’t set it.

Yes, getBasePath only returns the value you set with setBasePath($basePath). By defaults it’s empty.

Then what exactly do you mean by base application url?
Do you have a Slim 3 example?

I mean this method in slim 3

1 Like

really?? omg this is terrible

@fede My last answer from Aug '19 is not valid anymore, so I deleted it.

So what’s the answer of this question :smile:?

I deleted my answer with a middleware, but I guess most people are looking the RouteParser.

Example: Get url by route name:

use \Slim\Routing\RouteContext;

$routeParser = RouteContext::fromRequest($request)->getRouteParser();

$url = $routeParser->urlFor($routeName, $data, $queryParams)