CakePHP 3.8-Cookie删除

CakePHP 3.8-Cookie删除,cakephp,cookies,settings,cakephp-3.x,Cakephp,Cookies,Settings,Cakephp 3.x,我尝试为每个Cookie设置自动登录,但无法使用中显示的函数删除Cookie 我有一个登录函数来检查“持久性”,即“记住我”复选框。 如果选中,则创建Cookie,生成密钥,将其分配给用户,并将其与响应一起发送: UsersController::login() if($this->request->getData('persistence')){ $cookie=$this->Users->createLoginCookie($user,+1个月)); $this->response=$thi

我尝试为每个Cookie设置自动登录,但无法使用中显示的函数删除Cookie

我有一个登录函数来检查“持久性”,即“记住我”复选框。 如果选中,则创建Cookie,生成密钥,将其分配给用户,并将其与响应一起发送:

UsersController::login()

if($this->request->getData('persistence')){
$cookie=$this->Users->createLoginCookie($user,+1个月));
$this->response=$this->response->withCookie($cookie
);
}
返回$this->redirect($this->Auth->redirectUrl());
UsersTable::createLoginCookie()

公共函数createLoginCookie($user,$expiry){
$key=Text::uuid();
$user=$this->findByEmail($user['email'])->first();
$user->persistence\u key=$key;
$this->save($user);
$cookie=新cookie(
“持久性”、$key、,
新日期时间($到期),
'',
'',
假,,
假的
);
返回$cookie;
}
如果用户手动注销,我希望删除Cookie:

UsersController::logout()

if($cookie=$this->request->getCookie('persistence\u key')){
$this->response=$this->response->withExpiredCookie($cookie);
}
$this->Flash->success('您已注销');
返回$this->redirect($this->Auth->logout());
可悲的是,饼干不会被移除


有什么想法吗?

您的代码
$cookie=$this->request->getCookie('persistence\u key')
返回cookie值,而不是cookie本身。因此,将cookie值传递给
withExpiredCookie()
方法,而不是cookie名称。无论如何,传递cookie名称现在已经不推荐了

像这样的方法应该会奏效:

$cookie = new Cookie('persistence_key');
$this->response = $this->response->withExpiredCookie($cookie);
请注意,cookie路径还必须与创建cookie的方式相对应。由于我在localhost上有许多CakePHP应用程序,因此在创建cookie时,我倾向于指定cookie路径:

$cookie = new Cookie('my-cookie');
$cookie = $cookie->withPath(Router::url('/'));
所以我必须像这样破坏它们:

$cookie = new Cookie('my-cookie');
$cookie = $cookie->withPath(Router::url('/'));
$this->response = $this->response->withExpiredCookie($cookie);

它是使用ExpiredCookie调用调用
,还是该条件失败?可以从cookie集合中检索cookie对象,如
$this->request->getCookieCollection()->get('persistence_key')