Skip to content

Commit

Permalink
Issue #63 Support server name URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Mar 21, 2016
1 parent f9a1c4d commit 342ffbd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
19 changes: 16 additions & 3 deletions UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions tests/UrlCreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/<term:.+>/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([
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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/<term:.+>/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([
Expand Down Expand Up @@ -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/<term:.+>/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([
Expand Down

0 comments on commit 342ffbd

Please sign in to comment.