我是cakephp的新手,用全新的2.1建立我的第一个项目。
我使用auth和session组件来构建一个简单的登录系统。一切正常。我正在使用表单身份验证,自定义请求字段使用电子邮件作为登录。
当我尝试
时var_dump($this->Session->read('Auth.User'));
我可以看到会话中存储的所有用户数据,但我不需要这些全部。当然,我只能用我需要的数据子集重写会话,我的会话存储在memecache上,我不能浪费内存。
我希望能够轻松更新cakephp,所以我不想重写部分cakephp组件。
我正在寻找一个解决方案,让我指定会话中需要哪些字段。
答案 0 :(得分:4)
您可以像这样编写自己的身份验证组件:
class MyAuthComponent extends AuthComponent {
var $storeFields = array();
protected function cleanupUserData($user) {
if (empty($this->storeFields)) return $user;
retrun array_intersect_key($user, array_flip($this->storeFields));
}
public function login($user = null) {
$this->_setDefaults();
if (empty($user)) {
$user = $this->identify($this->request, $this->response);
}
if ($user) {
$this->Session->renew();
$user = $this->cleanupUserData($user);
$this->Session->write(self::$sessionKey, $user);
}
return $this->loggedIn();
}
}
然后在控制器中写这样的东西:
$this->MyAuth->storeFields = array('email', 'name', 'dob');