Building the Full Request URL

Am attempting to build the full request uri that used in Browser/UserAgent by the EndUser/Program.

  • Is this the correct way to do it, in Slim ?
  • Will it work under all circumstance ?

Inspected the URI object; could not find a single method that does this task. I mean … something like $uri->getFullUrl();

$container = $app->getContainer();
$request   = $container['request'];
$uri       = $request->getUri();

$baseUrl    = $uri->getBaseUrl();
$path       = $uri->getPath();
$query      = $uri->getQuery();
$pathFixed  = ($path && ($path[0] !== '/')) ? ('/' . $path) : ($path);
$queryFixed = ($query) ? ('?' . $query) : ($query);

$fullUrl = $baseUrl . $pathFixed . $queryFixed;

Samle Output(s) :

Thnaks in advance :slight_smile:

Susanth K

Try casting the Url instance to a string

1 Like


WOW ! That simple ! Thank you. It’s working.

$fullUrl = (string) $uri; // just TypeCast !!! <3 slim !

casting works fine even when basePath has a value or not ( empty ‘’ )

Though this casting trick is not documented in docs.
Hope this will not change in future release treating as an Undocumented feature. :frowning:

Thank You
Susanth K

This functionality is specified in PSR-7 so not very likely to change :wink:

1 Like

Yes! PSR-7 Support is a highlighted feature. So we can trust.

But this facility appear like a hidden feature. I wish, if below is possible.

$fullUrl =  $uri->getFullUrl();

let me raise a feature request @ github.

Edit :
Done : Building the Full Request URL like $uri->getFullUrl(); · Issue #1966 · slimphp/Slim · GitHub

Please up vote at github by :thumbsup: +1 or comment, if you support this implementation request.


Susanth K

Hi @JoeBengalen,

BTW; Never retrieve the request from the container. Always use the one given to the callable.

originally commented at GitHub. moved here, to make less cluttered there @github.

Yes. I prefer to use the given nearest $response object where ever applicable.

There are circumstances we need access globally / out of call back scope.

For example please see this thread ; There, inside function addRoutesForCurrentPath($app){ //... }.

Hope, in such situations, its perfectly valid to retrieve the request from the container.

Thank you
Susanth K

I would not recommend to use the request from the container. It will also be removed from the container in future a version of Slim.

The reason for this is that request is immutable. This means that changes on the request made in middleware (or whereever) is not reflected onto the request in the container, meaning you have an invalid outdated object!

[BC break] Remove Request, Response and Env from container

1 Like


Good clarification, with valid supporting links.

That thread Example could be implemented inside an App level middleware to meet the guidelines.

Thanking you

Susanth K