我是新來的CakePHP,並試圖讓我的頭一輪註冊/登錄,即時通訊使用CakePHP 2.0CakePHP的2 - 手動登錄不添加到會話
如果我使用寄存器控制器功能創建一個用戶,然後登錄seperately會話($ this-Auth-user())包含我期望的所有模型值。
如果我按照CakePHP的2.0書示例和寄存器中手動登錄然後我做獲得登錄而$這個 - > Auth->用戶()只包含從登記表不是用戶領域模型。 (即其缺少我的模型字段「創建」,「全名」等,但包括「密碼」和「密碼確認」都未加密)
我在這裏錯過了什麼嗎?
登錄操作:
public function login(){
if($this->request->is('post')){
if($this->Auth->login()){
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid Username or Password.'), 'default', array(), 'auth');
}
}
}
註冊行動(帶自動登錄):
public function register(){
if($this->request->is('post')){
$this->User->create();
if($this->User->save($this->request->data)){
//Login automatically
if($this->Auth->login($this->data['User'])){
$this->Session->setFlash(__('Welcome to Test Site.'), 'default', array(), 'auth');
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Failed to login with new account.'), 'default', array(), 'auth');
$this->redirect(array('action' => 'login'));
}
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
Register.ctp查看
<?php
echo $this->Form->create('User', array('action' => 'register'));
echo $this->Form->input('username');
echo $this->Form->input('password', array('label' => 'Password'));
echo $this->Form->input('passwordconfirm', array('label' => 'Confirm Password', 'type' => 'password'));
echo $this->Form->input('email', array('label' => 'Email (optional)'));
echo $this->Form->end('Join');
?>
乾杯任何幫助球員道歉這是一個有點長!
編輯:
找到一個解決辦法! 貌似$這 - > Auth->登錄($這 - >數據[ '用戶'])是未能AuthComponent ::識別()我的用戶,所以我改變了它爲以下3行:
$LoggedInID = $this->User->field('id', array('username' => $this->Auth->user('username')));
$user = $this->User->read(null, $LoggedInID);
$this->Auth->login($user);
和這個作品是一種享受。
編輯:你也可以使用保存()的返回值:
if($user = $this->User->save($this->request->data)){
//Login automatically
if($this->Auth->login($user)){