Skip to content

Commit

Permalink
Issue #48 Implement languageCookieOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Sep 17, 2015
1 parent 7784a52 commit 7c2544e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ You can modify other persistence settings with:
* `languageCookieDuration`: How long in seconds to store the language information in a cookie.
Set to `false` to disable the cookie.
* `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`.

#### Reset To Default Language
Expand Down
21 changes: 15 additions & 6 deletions UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class UrlManager extends BaseUrlManager
*/
public $languageCookieDuration = 2592000;

/**
* @var array configuration options for the language cookie. Note that `$languageCookieName`
* and `$languageCookeDuration` will override any `name` and `expire` settings provided here.
*/
public $languageCookieOptions = [];

/**
* @var array list of route and URL regex patterns to ignore during language processing. The keys
* of the array are patterns for routes, the values are patterns for URLs. Route patterns are checked
Expand Down Expand Up @@ -267,12 +273,15 @@ protected function processLocaleUrl($request)
Yii::$app->session[$this->languageSessionKey] = $language;
Yii::trace("Persisting language '$language' in session.", __METHOD__);
if ($this->languageCookieDuration) {
$cookie = new Cookie([
'name' => $this->languageCookieName,
'httpOnly' => true
]);
$cookie->value = $language;
$cookie->expire = time() + (int) $this->languageCookieDuration;
$cookie = new Cookie(array_merge(
['httpOnly' => true],
$this->languageCookieOptions,
[
'name' => $this->languageCookieName,
'value' => $language,
'expire' => time() + (int) $this->languageCookieDuration,
]
));
Yii::$app->getResponse()->getCookies()->add($cookie);
Yii::trace("Persisting language '$language' in cookie.", __METHOD__);
}
Expand Down

0 comments on commit 7c2544e

Please sign in to comment.