Skip to content

Commit

Permalink
Issue #39 Simplify redirectToLanguage()
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Aug 11, 2015
1 parent 7d3ef36 commit b4c9629
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 53 deletions.
57 changes: 11 additions & 46 deletions UrlManager.php
Expand Up @@ -355,59 +355,24 @@ protected function matchCode($code)
*/
protected function redirectToLanguage($language)
{
// Examples:
// 1) /baseurl/index.php/some/page?q=foo
// 2) /baseurl/some/page?q=foo
// 3)
// 4) /some/page?q=foo

if ($this->showScriptName) {
// 1) /baseurl/index.php
// 2) /baseurl/index.php
// 3) /index.php
// 4) /index.php
$redirectUrl = $this->_request->getScriptUrl();
} else {
// 1) /baseurl
// 2) /baseurl
// 3)
// 4)
$redirectUrl = $this->_request->getBaseUrl();
}

if ($language) {
$redirectUrl .= '/'.$language;
$result = parent::parseRequest($this->_request);
if ($result === false) {
throw new \yii\web\NotFoundHttpException(Yii::t('yii', 'Page not found.'));
}

// 1) some/page
// 2) some/page
// 3)
// 4) some/page
$pathInfo = $this->_request->getPathInfo();
if ($pathInfo) {
$redirectUrl .= '/'.$pathInfo;
list ($route, $params) = $result;
if($language){
$params[$this->languageParam]=$language;
}

if ($redirectUrl === '') {
$redirectUrl = '/';
}

// 1) q=foo
// 2) q=foo
// 3)
// 4) q=foo
$queryString = $this->_request->getQueryString();
if ($queryString) {
$redirectUrl .= '?'.$queryString;
}

Yii::$app->getResponse()->redirect($redirectUrl);
array_unshift($params, $route);
$url = $this->createUrl($params);
Yii::$app->getResponse()->redirect($url);
if (YII_ENV_TEST) {
// Response::redirect($url) above will call `Url::to()` internally. So to really
// test for the same final redirect URL here, we need to call Url::to(), too.
throw new \yii\base\Exception(\yii\helpers\Url::to($redirectUrl));
throw new \yii\base\Exception(\yii\helpers\Url::to($url));
} else {
Yii::$app->end();
}

}
}
6 changes: 1 addition & 5 deletions tests/TestCase.php
Expand Up @@ -69,15 +69,11 @@ public function mockComponent($config = []) {
/**
* Expect a redirect exception
*
* If an empty string is passed, `showScriptName` is `false` and the `baseUrl` is empty, a `/`
* is expected. In all other cases a URL depending on `showScriptName` and `baseUrl` without a
* trailing slash is expected.
*
* @param string $url the redirect URL
*/
protected function expectRedirect($url)
{
$url = $this->prepareUrl($url) ? : '/';
$url = $this->prepareUrl($url);
$this->setExpectedExceptionRegExp('\yii\base\Exception', '#^' . $url . '$#');
}

Expand Down
4 changes: 2 additions & 2 deletions tests/UrlManagerTest.php
Expand Up @@ -86,7 +86,7 @@ public function testRedirectsIfDefaultLanguageInUrlAndDefaultLanguageUsesNoSuffi

public function testRedirectsToRootIfOnlyDefaultLanguageInUrlAndDefaultLanguageUsesNoSuffix()
{
$this->expectRedirect('');
$this->expectRedirect('/');
$this->mockRequest('/en');
$this->mockComponent([
'languages' => ['en-US', 'en', 'de'],
Expand All @@ -105,7 +105,7 @@ public function testRedirectsIfNoLanguageInUrlAndDefaultLanguageUsesSuffix()

public function testRedirectsIfDefaultLanguageInUrl()
{
$this->expectRedirect('');
$this->expectRedirect('/');
$this->mockRequest('/en');
$this->mockComponent([
'languages' => ['en'],
Expand Down

0 comments on commit b4c9629

Please sign in to comment.