我试图了解Cakephp的工作原理。我用一个简单的表单(用户名和密码)构建了一个连接系统。当我点击提交按钮(使用正确的用户名和密码)时,我总是收到相同的flash消息,"您的用户名或密码不正确"。我尝试了很多东西,但我不知道如何解决它。 如果我已经登录,我想要重定向到谷歌(这是我尝试的测试)。
看看我的代码:
login.ctp(查看)
<?php
echo $this->Form->create('User',array('action'=>'login'));
echo $this->Form->inputs(array('legend'=>'Se connecter','username','password'));
echo $this->Form->end('Se connecter');
?>
UsersController
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow();
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->User->id = $this->Auth->user("id");
$this->redirect('http://www.google.fr');
} else {
$this->Session->setFlash('Your username or password was incorrect.');
}
}
}
}
用户(型号)
<?php
App::uses('AppModel', 'Model', 'AuthComponent', 'Controller/Component');
class User extends AppModel {
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public function beforeSave($options = array()) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
return true;
}
public $belongsTo = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
?>
我的表用户(sql)中的密码:
password char(40) latin1_swedish_ci