1.不要使用相同的request url, 否则需要刷新才能获取值
可在不同的动作中设置和调用2.不要在函数中使用exit来中止脚本的执行,否则设置值无效
3.删除:在不同的动作中调用
\yii::$app->response->cookies->remove(‘xxxx‘);
public function actionSetCookie() {
$cookies = Yii::$app->response->cookies;
$cookies->add(new \yii\web\Cookie([
‘name‘ => ‘abc‘,
‘value‘ => ‘xyz‘,
‘expire‘ => time() + 86400 * 365,
]));
echo ‘Cookie set!‘;
}
public function actionGetCookie() {
$cookies1 = Yii::$app->request->cookies;
if ($cookies1->has(‘abc‘))
$cookieValue = $cookies1->getValue(‘abc‘);
echo ‘value : ‘.$cookieValue;
}
public function actionDeleteCookie(){
\yii::$app->response->cookies->remove(‘carts‘);
}