Skip to content

Commit

Permalink
Merge pull request #128 from codemix/127-optional-session
Browse files Browse the repository at this point in the history
Make use of session optional
  • Loading branch information
mikehaertl authored Feb 6, 2018
2 parents 645d62a + c70ea3a commit 23397f9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ php:
install:
- sudo apt-get update
- travis_retry composer self-update && composer --version
- travis_retry composer global require --no-progress "fxp/composer-asset-plugin:^1.4.1"
- travis_retry composer clear-cache
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction --no-progress
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ You can modify other persistence settings with:
* `languageCookieName`: The name of the language cookie. Default is `_language`.
* `languageCookieOptions`: Other options to set on the language cookie.
* `languageSessionKey`: The name of the language session key. Default is `_language`.
Since 1.6.0 this can also be set to `false` to not use the session at all.

#### Reset To Default Language

Expand Down
17 changes: 11 additions & 6 deletions UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class UrlManager extends BaseUrlManager
public $keepUppercaseLanguageCode = false;

/**
* @var string the name of the session key that is used to store the
* language. Default is '_language'.
* @var string|bool the name of the session key that is used to store the
* language. If `false` no session is used. Default is '_language'.
*/
public $languageSessionKey = '_language';

Expand Down Expand Up @@ -418,8 +418,10 @@ protected function persistLanguage($language)
]));
}
}
Yii::$app->session[$this->languageSessionKey] = $language;
Yii::trace("Persisting language '$language' in session.", __METHOD__);
if ($this->languageSessionKey !== false) {
Yii::$app->session[$this->languageSessionKey] = $language;
Yii::trace("Persisting language '$language' in session.", __METHOD__);
}
if ($this->languageCookieDuration) {
$cookie = new Cookie(array_merge(
['httpOnly' => true],
Expand All @@ -440,8 +442,11 @@ protected function persistLanguage($language)
*/
protected function loadPersistedLanguage()
{
$language = Yii::$app->session->get($this->languageSessionKey);
$language!==null && Yii::trace("Found persisted language '$language' in session.", __METHOD__);
$language = null;
if ($this->languageSessionKey !== false) {
$language = Yii::$app->session->get($this->languageSessionKey);
$language!==null && Yii::trace("Found persisted language '$language' in session.", __METHOD__);
}
if ($language===null) {
$language = $this->_request->getCookies()->getValue($this->languageCookieName);
$language!==null && Yii::trace("Found persisted language '$language' in cookie.", __METHOD__);
Expand Down
14 changes: 14 additions & 0 deletions tests/UrlManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,18 @@ public function testCanDisableCookieOnly()
$request = Yii::$app->request;
$this->assertEquals('site/page', $request->pathInfo);
}

public function testCanDisableSessionOnly()
{
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
'languageSessionKey' => false,
]);
$this->mockRequest('/en-us/site/page');
$this->assertEquals('en-US', Yii::$app->language);
$this->assertNull(Yii::$app->session->get('_language'));
$this->assertEquals('en-US', Yii::$app->response->cookies->get('_language'));
$request = Yii::$app->request;
$this->assertEquals('site/page', $request->pathInfo);
}
}

0 comments on commit 23397f9

Please sign in to comment.