yii2 new cookies code example
Example 1: yii2 get cookie
$cookies = Yii::$app->request->cookies;
$language = $cookies->getValue('language', 'en');
if (($cookie = $cookies->get('language')) !== null) {
$language = $cookie->value;
}
if (isset($cookies['language'])) {
$language = $cookies['language']->value;
}
if ($cookies->has('language')) ...
if (isset($cookies['language'])) ...
Example 2: yii2 set cookie
$cookies = Yii::$app->response->cookies;
$cookies->add(new \yii\web\Cookie([
'name' => 'language',
'value' => 'zh-CN',
]));
$cookies->remove('language');
unset($cookies['language']);