记住我在CakePHP 3中使用身份验证

时间:2015-12-15 12:33:13

标签: cookies cakephp-3.x

如何在CakePHP 3中使用auth的“记住我”cookie? 我在我的控制器中使用了以下代码:

if ($this->request->data['data']['rememberMe'] == "on") {
    $cookie = array();
    $cookie['username'] = $this->request->data['username'];
    $cookie['password'] = $this->request->data['password']; 
    $this->request->Cookie->write('rememberMe', $cookie, true, "1 week");
    unset($this->request->data['rememberMe']);
}

并收到错误:

  

在非对象上调用成员函数write()

1 个答案:

答案 0 :(得分:1)

就像错误说的那样。你应该写这样的cookie:

$ this-> Cookie-> write();

有关更多详细信息,请参阅此链接:http://book.cakephp.org/3.0/en/controllers/components/cookie.html#Cake \ Controller \ Component \ CookieComponent :: write