Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[Request] Added a getBaseServerUrl() function #3416

Closed
wants to merge 6 commits into from

5 participants

@COil

Here is a use case for this little PR. I wanted to have the full URL for an image related to an object (relative URL is stored into the 'webPath' property of the 'job' object)

<div>
    <a href="{{ job.url }}">
        <img src="{{ app.request.scheme ~ '://' ~ app.request.host }}{{ asset(job.webPath) }}" alt="{{ job.company }} logo" />
    </a>
</div>

This twig code is quiet complicated just to retrieve the base URi... so with the PR the code turns into:

<div>
    <a href="{{ job.url }}">
        <img src="{{ app.request.getBaseUri }}{{ asset(job.webPath) }}" alt="{{ job.company }} logo" />
    </a>
</div>

Which is far more compact. Additional unit tests are included.

COil.

src/Symfony/Component/HttpFoundation/Request.php
@@ -744,7 +744,19 @@ public function getUri()
*/
public function getUriForPath($path)
{
- return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$path;
+ return $this->getBaseUri().$this->getBaseUrl().$path;
+ }
+
+ /**
+ * Generates the base URI.
+ *
+ * @return string The URI for the base public path
+ *
+ * @api
+ */
+ public function getBaseUri()
+ {
+ return $this->getScheme().'://'.$this->getHttpHost();
@stof Collaborator
stof added a note

This is not the base uri. The base uri should include the base path necessary to reach your web root

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@COil

Humm, you are right Stof ! I knew I add forgotten something... :x Well in fact, getUriForPath('') is what I want, It works in prod env (without the controller) but it's not correct when the controller is used in the URL. So what is the easiest way to handle this ? Compute this path after prepareBaseUrl() ?

Other user who add a similar question:
http://groups.google.com/group/symfony2/browse_thread/thread/6a6f14ad88e3fc0?pli=1

@stof
Collaborator

Well, getUriForPath is meant to give the uri for a path inside the app, so it is logical to include the front controller in it.

@COil

So, how to write the following code ?

<div>
    <a href="{{ job.url }}">
        <img src="{{ app.request.scheme ~ '://' ~ app.request.host }}{{ asset(job.webPath) }}" alt="{{ job.company }} logo" />
    </a>
</div>

It's wrong too if the root of the web application is not /.

@stof
Collaborator

@COil no it is not, as the asset function is precisely meant to deal with it

@COil

Yes, you are right. So the name of my function is wrong, it should be something like getBaseServerUrl()

@dlsniper

Hi, any news about this?

README.md
@@ -1,7 +1,7 @@
README
======
-[![Build Status](https://secure.travis-ci.org/symfony/symfony.png?branch=master)](http://travis-ci.org/symfony/symfony)
+[![Build Status](https://secure.travis-ci.org/COil/symfony.png?branch=master)](http://travis-ci.org/COil/symfony)
@lsmith77 Collaborator

that needs to be reverted

@COil
COil added a note

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof
Collaborator

@COil Please rebase your branch. It conflicts with master because of the move of tests. You should probably squash the commits too.

@fabpot what do you think about it ?

@dinamic

Why not an optional parameter to the asset() twig function?

@COil

@dinamic Indeed, it could be an improvement from the Twig side

@stof I have created a new clean PR #4312

@stof stof closed this
@fabpot fabpot referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@fabpot fabpot referenced this pull request from a commit in fabpot/symfony
@fabpot fabpot added Request::getSchemeAndHttpHost() and Request::getUserInfo() (clo…
…ses #4312, refs #3416, refs #3056)
df8d94e
@fabpot fabpot referenced this pull request from a commit
@fabpot fabpot merged branch fabpot/request-methods (PR #4679)
Commits
-------

df8d94e added Request::getSchemeAndHttpHost() and Request::getUserInfo() (closes #4312, refs #3416, refs #3056)

Discussion
----------

added Request::getSchemeAndHttpHost() and Request::getUserInfo()

see #4312

---------------------------------------------------------------------------

by travisbot at 2012-06-28T15:22:03Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1730172) (merged 598bd56f into 0d27570).

---------------------------------------------------------------------------

by Seldaek at 2012-06-28T15:22:35Z

Why not just `getSchemeAndHost`? That sounds long enough, and is fairly explicit given the context.

---------------------------------------------------------------------------

by fabpot at 2012-06-28T15:25:34Z

@Seldaek because (and that's probably unfortunate) we have both `getHost()` and `getHttpHost()`. The former does not include the port whereas the latter does.

---------------------------------------------------------------------------

by Seldaek at 2012-06-28T15:26:42Z

Ok makes sense.

---------------------------------------------------------------------------

by travisbot at 2012-06-28T16:11:16Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1730630) (merged df8d94e into 884a826).
e0351c9
@dustinwhittle dustinwhittle referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@mmucklo mmucklo referenced this pull request from a commit
@fabpot fabpot merged branch fabpot/request-methods (PR #4679)
Commits
-------

df8d94e added Request::getSchemeAndHttpHost() and Request::getUserInfo() (closes #4312, refs #3416, refs #3056)

Discussion
----------

added Request::getSchemeAndHttpHost() and Request::getUserInfo()

see #4312

---------------------------------------------------------------------------

by travisbot at 2012-06-28T15:22:03Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1730172) (merged 598bd56f into 0d27570).

---------------------------------------------------------------------------

by Seldaek at 2012-06-28T15:22:35Z

Why not just `getSchemeAndHost`? That sounds long enough, and is fairly explicit given the context.

---------------------------------------------------------------------------

by fabpot at 2012-06-28T15:25:34Z

@Seldaek because (and that's probably unfortunate) we have both `getHost()` and `getHttpHost()`. The former does not include the port whereas the latter does.

---------------------------------------------------------------------------

by Seldaek at 2012-06-28T15:26:42Z

Ok makes sense.

---------------------------------------------------------------------------

by travisbot at 2012-06-28T16:11:16Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1730630) (merged df8d94e into 884a826).
8dad8dc
@hackzilla hackzilla referenced this pull request from a commit
@fabpot fabpot merged branch fabpot/request-methods (PR #4679)
Commits
-------

df8d94e added Request::getSchemeAndHttpHost() and Request::getUserInfo() (closes #4312, refs #3416, refs #3056)

Discussion
----------

added Request::getSchemeAndHttpHost() and Request::getUserInfo()

see #4312

---------------------------------------------------------------------------

by travisbot at 2012-06-28T15:22:03Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1730172) (merged 598bd56f into 0d27570).

---------------------------------------------------------------------------

by Seldaek at 2012-06-28T15:22:35Z

Why not just `getSchemeAndHost`? That sounds long enough, and is fairly explicit given the context.

---------------------------------------------------------------------------

by fabpot at 2012-06-28T15:25:34Z

@Seldaek because (and that's probably unfortunate) we have both `getHost()` and `getHttpHost()`. The former does not include the port whereas the latter does.

---------------------------------------------------------------------------

by Seldaek at 2012-06-28T15:26:42Z

Ok makes sense.

---------------------------------------------------------------------------

by travisbot at 2012-06-28T16:11:16Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1730630) (merged df8d94e into 884a826).
d653bbd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 22, 2012
  1. @COil
Commits on Mar 1, 2012
  1. @COil

    Renamed function

    COil authored
Commits on Mar 6, 2012
  1. @COil

    Changes Travis-ci URL

    COil authored
  2. @COil

    Update README.md

    COil authored
Commits on Mar 27, 2012
  1. @COil
  2. @COil

    Fixed DocBlock

    COil authored
This page is out of date. Refresh to see the latest.
View
16 src/Symfony/Component/HttpFoundation/Request.php
@@ -744,7 +744,19 @@ public function getUri()
*/
public function getUriForPath($path)
{
- return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$path;
+ return $this->getBaseServerUrl().$this->getBaseUrl().$path;
+ }
+
+ /**
+ * Generates the absolute base server URL.
+ *
+ * @return string The base URL of the server
+ *
+ * @api
+ */
+ public function getBaseServerUrl()
+ {
+ return $this->getScheme().'://'.$this->getHttpHost();
}
/**
@@ -1228,7 +1240,7 @@ protected function prepareRequestUri()
} elseif ($this->server->has('REQUEST_URI')) {
$requestUri = $this->server->get('REQUEST_URI');
// HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
- $schemeAndHttpHost = $this->getScheme().'://'.$this->getHttpHost();
+ $schemeAndHttpHost = $this->getBaseServerUrl();
if (strpos($requestUri, $schemeAndHttpHost) === 0) {
$requestUri = substr($requestUri, strlen($schemeAndHttpHost));
}
View
63 tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php
@@ -409,6 +409,69 @@ public function testGetUriForPath()
}
/**
+ * @covers Symfony\Component\HttpFoundation\Request::getBaseServerUrl
+ */
+ public function testgetBaseServerUrl()
+ {
+ $request = Request::create('http://test.com/foo?bar=baz');
+ $this->assertEquals('http://test.com', $request->getBaseServerUrl());
+
+ $request = Request::create('http://test.com:90/foo?bar=baz');
+ $this->assertEquals('http://test.com:90', $request->getBaseServerUrl());
+
+ $server = array();
+
+ // Standard Request on non default PORT
+ // http://hostname:8080/index.php/path/info?query=string
+
+ $server['HTTP_HOST'] = 'hostname:8080';
+ $server['SERVER_NAME'] = 'servername';
+ $server['SERVER_PORT'] = '8080';
+
+ $server['QUERY_STRING'] = 'query=string';
+ $server['REQUEST_URI'] = '/index.php/path/info?query=string';
+ $server['SCRIPT_NAME'] = '/index.php';
+ $server['PATH_INFO'] = '/path/info';
+ $server['PATH_TRANSLATED'] = 'redirect:/index.php/path/info';
+ $server['PHP_SELF'] = '/index_dev.php/path/info';
+ $server['SCRIPT_FILENAME'] = '/some/where/index.php';
+
+ $request = new Request();
+
+ $request->initialize(array(), array(), array(), array(), array(),$server);
+
+ $this->assertEquals('http://hostname:8080', $request->getBaseServerUrl(), '->getBaseServerUrl() with non default port');
+
+ // Use std port number
+ $server['HTTP_HOST'] = 'hostname';
+ $server['SERVER_NAME'] = 'servername';
+ $server['SERVER_PORT'] = '80';
+
+ $request->initialize(array(), array(), array(), array(), array(), $server);
+
+ $this->assertEquals('http://hostname', $request->getBaseServerUrl(), '->getBaseServerUrl() with default port');
+
+ // With a sub directory for the public web root
+ $server['HTTP_HOST'] = 'hostname';
+ $server['SERVER_NAME'] = 'servername';
+ $server['SERVER_PORT'] = '80';
+
+ $server['QUERY_STRING'] = 'query=string';
+ $server['REQUEST_URI'] = '/web/index.php/path/info?query=string';
+ $server['SCRIPT_NAME'] = '/index.php';
+ $server['PATH_INFO'] = '/path/info';
+ $server['PATH_TRANSLATED'] = 'redirect:/web/index.php/path/info';
+ $server['PHP_SELF'] = '/web/index_dev.php/path/info';
+ $server['SCRIPT_FILENAME'] = '/some/where/index.php';
+
+ $request = new Request();
+
+ $request->initialize(array(), array(), array(), array(), array(), $server);
+
+ $this->assertEquals('http://hostname', $request->getBaseServerUrl(), '->getBaseServerUrl() does not take care of the base path');
+ }
+
+ /**
* @covers Symfony\Component\HttpFoundation\Request::getQueryString
*/
public function testGetQueryString()
Something went wrong with that request. Please try again.