Url: Fix detection of the current base url when stripping it from a given path

This commit is contained in:
Johannes Meyer 2015-10-01 10:38:22 +02:00
parent 5f7f0a9e89
commit 85e7e7920a
2 changed files with 9 additions and 9 deletions

View File

@ -161,9 +161,12 @@ class Url
if ($urlPath && $urlPath[0] === '/') { if ($urlPath && $urlPath[0] === '/') {
if ($baseUrl) { if ($baseUrl) {
$urlPath = substr($urlPath, 1); $urlPath = substr($urlPath, 1);
} elseif (strpos($urlPath, $request->getBaseUrl()) === 0) { } else {
$baseUrl = $request->getBaseUrl(); $requestBaseUrl = $request->getBaseUrl();
$urlPath = substr($urlPath, strlen($baseUrl) + 1); if ($requestBaseUrl && $requestBaseUrl !== '/' && strpos($urlPath, $requestBaseUrl) === 0) {
$urlPath = substr($urlPath, strlen($requestBaseUrl) + 1);
$baseUrl = $requestBaseUrl;
}
} }
} elseif (! $baseUrl) { } elseif (! $baseUrl) {
$baseUrl = $request->getBaseUrl(); $baseUrl = $request->getBaseUrl();

View File

@ -158,17 +158,14 @@ class UrlTest extends BaseTestCase
); );
} }
/** public function testWhetherGetRelativeUrlReturnsTheEmptyStringForAbsoluteUrls()
* @depends testWhetherFromPathProperlyRecognizesAndDecodesQueryParameters
*/
public function testWhetherGetRelativeUrlReturnsTheRelativeUrl()
{ {
$url = Url::fromPath('/my/test/url.html?param=val&param2=val2'); $url = Url::fromPath('/my/test/url.html?param=val&param2=val2');
$this->assertEquals( $this->assertEquals(
'my/test/url.html?param=val&param2=val2', '',
$url->getRelativeUrl(), $url->getRelativeUrl(),
'Url::getRelativeUrl does not return the relative url' 'Url::getRelativeUrl does not return the empty string for absolute urls'
); );
} }