From 342ffbd5f5d9a1264a82f21ccc669ebcc3102703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Mon, 21 Mar 2016 22:06:23 +0100 Subject: [PATCH] Issue #63 Support server name URLs --- UrlManager.php | 19 +++++++++++--- tests/UrlCreationTest.php | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/UrlManager.php b/UrlManager.php index ac70993..5bd6afe 100644 --- a/UrlManager.php +++ b/UrlManager.php @@ -218,8 +218,6 @@ public function createUrl($params) return $url; } else { $key = array_search($language, $this->languages); - $base = $this->showScriptName ? $this->getScriptUrl() : $this->getBaseUrl(); - $length = strlen($base); if (is_string($key)) { $language = $key; } @@ -234,7 +232,22 @@ public function createUrl($params) $url = rtrim($url, '/'); } } - return $length ? substr_replace($url, "$base/$language", 0, $length) : "/$language$url"; + + // /foo/bar -> /de/foo/bar + // /base/url/foo/bar -> /base/url/de/foo/bar + // /base/index.php/foo/bar -> /base/index.php/de/foo/bar + // http://www.example.com/base/url/foo/bar -> http://www.example.com/base/de/foo/bar + $needle = $this->showScriptName ? $this->getScriptUrl() : $this->getBaseUrl(); + // Check for server name URL + if (strpos($url, '://')!==false) { + if (($pos = strpos($url, '/', 8))!==false || ($pos = strpos($url, '?', 8))!==false) { + $needle = substr($url, 0, $pos) . $needle; + } else { + $needle = $url . $needle; + } + } + $needleLength = strlen($needle); + return $needleLength ? substr_replace($url, "$needle/$language", 0, $needleLength) : "/$language$url"; } } else { return parent::createUrl($params); diff --git a/tests/UrlCreationTest.php b/tests/UrlCreationTest.php index 40547bf..eb2d57e 100644 --- a/tests/UrlCreationTest.php +++ b/tests/UrlCreationTest.php @@ -76,6 +76,19 @@ public function testCreateAbsoluteUrlWithLanguageFromUrl() $this->assertEquals('http://localhost'.$this->prepareUrl('/de/foo/baz/bar?x=y'), Url::to(['/slug/action', 'x' => 'y', 'term' => 'baz'], 'http')); } + public function testCreateServerNameUrlWithLanguageFromUrl() + { + $this->mockUrlManager([ + 'languages' => ['en-US', 'en', 'de'], + 'rules' => [ + 'http://www.example.com/foo//bar' => 'slug/action', + ], + ]); + $this->mockRequest('/de/site/page'); + $this->assertEquals('http://www.example.com'.$this->prepareUrl('/de/foo/baz/bar'), Url::to(['/slug/action', 'term' => 'baz'])); + $this->assertEquals('http://www.example.com'.$this->prepareUrl('/de/foo/baz/bar?x=y'), Url::to(['/slug/action', 'x' => 'y', 'term' => 'baz'])); + } + public function testCreateUrlWithLanguageFromUrlIfUppercaseEnabled() { $this->mockUrlManager([ @@ -120,6 +133,18 @@ public function testCreateAbsoluteHomeUrlWithLanguageFromUrl() $this->assertEquals('http://localhost'.$this->prepareUrl('/de?x=y'), Url::to(['/site/index', 'x' => 'y'], 'http')); } + public function testCreateServerNameHomeUrlWithLanguageFromUrl() + { + $this->mockUrlManager([ + 'languages' => ['en-US', 'en', 'de'], + 'rules' => [ + 'http://www.example.com' => 'site/index', + ], + ]); + $this->mockRequest('/de/site/page'); + $this->assertEquals('http://www.example.com'.$this->prepareUrl('/de'), Url::to(['/site/index'])); + } + public function testCreateUrlWithSpecificLanguage() { $this->mockUrlManager([ @@ -152,6 +177,19 @@ public function testCreateAbsoluteUrlWithSpecificLanguage() $this->assertEquals('http://localhost'.$this->prepareUrl('/en-us/foo/baz/bar?x=y'), Url::to(['/slug/action', 'language' => 'en-US', 'x' => 'y', 'term' => 'baz'], 'http')); } + public function testCreateServerNameUrlWithSpecificLanguage() + { + $this->mockUrlManager([ + 'languages' => ['en-US', 'en', 'de'], + 'rules' => [ + 'http://www.example.com/foo//bar' => 'slug/action', + ], + ]); + $this->mockRequest('/de/site/page'); + $this->assertEquals('http://www.example.com'.$this->prepareUrl('/en-us/foo/baz/bar'), Url::to(['/slug/action', 'language' => 'en-US', 'term' => 'baz'])); + $this->assertEquals('http://www.example.com'.$this->prepareUrl('/en-us/foo/baz/bar?x=y'), Url::to(['/slug/action', 'language' => 'en-US', 'x' => 'y', 'term' => 'baz'])); + } + public function testCreateUrlWithSpecificAliasedLanguage() { $this->mockUrlManager([ @@ -234,6 +272,20 @@ public function testCreateAbsoluteUrlWithTralingSlashWithLanguageFromUrl() $this->assertEquals('http://localhost'.$this->prepareUrl('/de/foo/baz/bar/?x=y'), Url::to(['/slug/action', 'x' => 'y', 'term' => 'baz'], 'http')); } + public function testCreateServerNameUrlWithTralingSlashWithLanguageFromUrl() + { + $this->mockUrlManager([ + 'languages' => ['en-US', 'en', 'de'], + 'suffix' => '/', + 'rules' => [ + 'http://www.example.com/foo//bar' => 'slug/action', + ], + ]); + $this->mockRequest('/de/site/page/'); + $this->assertEquals('http://www.example.com'.$this->prepareUrl('/de/foo/baz/bar/'), Url::to(['/slug/action', 'term' => 'baz'])); + $this->assertEquals('http://www.example.com'.$this->prepareUrl('/de/foo/baz/bar/?x=y'), Url::to(['/slug/action', 'x' => 'y', 'term' => 'baz'])); + } + public function testCreateHomeUrlWithTrailingSlashWithLanguageFromUrl() { $this->mockUrlManager([