Yii2 Get Cookie

[Solved] Yii2 Get Cookie | Php Frameworks Yii - Code Explorer | yomemimo.com
Question : yii2 get cookie

Answered by : aylmer-dela-cruz

// get the cookie collection (yii\web\CookieCollection) from the "request" component
$cookies = Yii::$app->request->cookies;
// get the "language" cookie value. If the cookie does not exist, return "en" as the default value.
$language = $cookies->getValue('language', 'en');
// an alternative way of getting the "language" cookie value
if (($cookie = $cookies->get('language')) !== null) { $language = $cookie->value;
}
// you may also use $cookies like an array
if (isset($cookies['language'])) { $language = $cookies['language']->value;
}
// check if there is a "language" cookie
if ($cookies->has('language')) ...
if (isset($cookies['language'])) ...

Source : https://www.yiiframework.com/doc/guide/2.0/en/runtime-sessions-cookies | Last Update : Tue, 15 Sep 20

Question : yii2 set cookie

Answered by : aylmer-dela-cruz

// get the cookie collection (yii\web\CookieCollection) from the "response" component
$cookies = Yii::$app->response->cookies;
// add a new cookie to the response to be sent
$cookies->add(new \yii\web\Cookie([ 'name' => 'language', 'value' => 'zh-CN',
]));
// remove a cookie
$cookies->remove('language');
// equivalent to the following
unset($cookies['language']);

Source : https://www.yiiframework.com/doc/guide/2.0/en/runtime-sessions-cookies | Last Update : Tue, 15 Sep 20

Answers related to yii2 get cookie

Code Explorer Popular Question For Php Frameworks Yii