yii2 get cookie

Solutions on MaxInterview for yii2 get cookie by the best coders in the world

showing results for - "yii2 get cookie"
Johanna
20 Jun 2016
1// get the cookie collection (yii\web\CookieCollection) from the "request" component
2$cookies = Yii::$app->request->cookies;
3
4// get the "language" cookie value. If the cookie does not exist, return "en" as the default value.
5$language = $cookies->getValue('language', 'en');
6
7// an alternative way of getting the "language" cookie value
8if (($cookie = $cookies->get('language')) !== null) {
9    $language = $cookie->value;
10}
11
12// you may also use $cookies like an array
13if (isset($cookies['language'])) {
14    $language = $cookies['language']->value;
15}
16
17// check if there is a "language" cookie
18if ($cookies->has('language')) ...
19if (isset($cookies['language'])) ...
20
Marie
22 Jan 2020
1// get the cookie collection (yii\web\CookieCollection) from the "response" component
2$cookies = Yii::$app->response->cookies;
3
4// add a new cookie to the response to be sent
5$cookies->add(new \yii\web\Cookie([
6    'name' => 'language',
7    'value' => 'zh-CN',
8]));
9
10// remove a cookie
11$cookies->remove('language');
12// equivalent to the following
13unset($cookies['language']);
14