# Building the Full Request URL

**URL:** <https://discourse.slimframework.com/t/building-the-full-request-url/626>\
**Category:** Questions\
**Created:** [August 17, 2016, 1:18pm UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626 "2016-08-17T13:18:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Susanth.Com](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/susanth.com/32/193_2.png) [@Susanth.Com](https://discourse.slimframework.com/u/Susanth.Com)\
**Post date:** [August 17, 2016, 1:18pm UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626/1 "2016-08-17T13:18:49Z")

</div>

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();`

```php
$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) : 
    http://example.com/
    http://example.com/foo
    http://example.com/foo/
    http://example.com/foo/bar
    http://example.com/foo/bar/
    http://example.com/foo/bar?a=1&b=2&c=3
    http://example.com/foo/bar/?a=1&b=2&c=3

    http://www.example.com/
    https://www.example.com/
    https://doc.example.com/
    http://www.example.com:1234/foo
    https://www.example.com:1234/foo
*/

```

Thnaks in advance 🙂

Susanth K

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [August 17, 2016, 2:15pm UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626/2 "2016-08-17T14:15:12Z")

</div>

Try casting the Url instance to a string

---

<div class="post-metadata">

**Author:** ![Susanth.Com](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/susanth.com/32/193_2.png) [@Susanth.Com](https://discourse.slimframework.com/u/Susanth.Com)\
**Post date:** [August 17, 2016, 2:21pm UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626/3 "2016-08-17T14:21:14Z")

</div>

@JoeBengalen

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

```php
$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](http://www.slimframework.com/docs/)._  
Hope this will not change in future release treating as an Undocumented feature. ☹

Thank You  
Susanth K

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [August 17, 2016, 3:59pm UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626/4 "2016-08-17T15:59:03Z")

</div>

> [@Susanth.Com](#):
>
> Though this casting trick is not documented in docs.Hope this will not change in future release treating as an Undocumented feature. ☹

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

---

<div class="post-metadata">

**Author:** ![Susanth.Com](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/susanth.com/32/193_2.png) [@Susanth.Com](https://discourse.slimframework.com/u/Susanth.Com)\
**Post date:** [August 18, 2016, 5:50am UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626/5 "2016-08-18T05:50:30Z")

</div>

> [@JoeBengalen](#):
>
> This functionality is specified in PSR-7 so not very likely to change 😉

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.

```php
$fullUrl = $uri->getFullUrl();

```

let me raise a feature request @ github.

Edit :  
Done : [https://github.com/slimphp/Slim/issues/1966](https://github.com/slimphp/Slim/issues/1966)

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

Thanks

Susanth K

---

<div class="post-metadata">

**Author:** ![Susanth.Com](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/susanth.com/32/193_2.png) [@Susanth.Com](https://discourse.slimframework.com/u/Susanth.Com)\
**Post date:** [August 18, 2016, 7:42am UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626/6 "2016-08-18T07:42:42Z")

</div>

Hi @JoeBengalen,

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

> _originally commented at [GitHub](https://github.com/slimphp/Slim/issues/1966). 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](http://discourse.slimframework.com/t/conditional-route-registering-based-on-current-request/617) ; There, inside `function addRoutesForCurrentPath($app){ //... }`.

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

Thank you  
Susanth K

---

<div class="post-metadata">

**Author:** ![JoeBengalen](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@JoeBengalen](https://discourse.slimframework.com/u/JoeBengalen)\
**Post date:** [August 18, 2016, 7:50am UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626/7 "2016-08-18T07:50:41Z")

</div>

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  
> [https://github.com/slimphp/Slim/issues/1686](https://github.com/slimphp/Slim/issues/1686)

---

<div class="post-metadata">

**Author:** ![Susanth.Com](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/susanth.com/32/193_2.png) [@Susanth.Com](https://discourse.slimframework.com/u/Susanth.Com)\
**Post date:** [August 18, 2016, 7:55am UTC](https://discourse.slimframework.com/t/building-the-full-request-url/626/8 "2016-08-18T07:55:10Z")

</div>

@JoeBengalen

Good clarification, with valid supporting links.

[That thread](http://discourse.slimframework.com/t/conditional-route-registering-based-on-current-request/617) Example could be implemented inside an App level middleware to meet the guidelines.

Thanking you

Susanth K
