Slim 4 get base url

Hi.
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 ?
Thanks.

1 Like

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

Example:

$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 https://github.com/slimphp/Slim/blob/200c6143f15baa477601879b64ab2326847aac0b/Slim/Http/Uri.php#L825.

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)

https://www.slimframework.com/docs/v4/objects/request.html#obtain-base-path-from-within-route

This might help you guys: Running Slim 4 in a subdirectory – Rob Allen