嗨,我使用的是 cakePHP 版本 2.3.6,我一直在尝试创建注册和登录,但是当我使用注册的用户名和密码登录时,它一直说错误的用户名或密码,而且我使用电子邮件作为用户名。请帮帮我谢谢。
AppController
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages''action' =>'display', 'home')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
View Login
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php echo $this->Form->input('email');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
UserController
<?php
class usersController extends AppController
{
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add','login','logout');
}
var $name = 'Users';
public function view($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->set('user', $this->User->read(null, $id));
}
public function add()
{
if (!empty($this ->data))
{
$this->User->create();
if ($this->User->save($this->data))
{
$this->Session->setFlash('Thank you for registering');
$this->redirect(array('action'=>'index'));
}
}
}
function index()
{
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login())
{
$this->redirect($this->Auth->redirectUrl());
}
else
{
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
}
**UserModel**
App::uses('AuthComponent','Controller/Component');
class User extends AppModel
{
var $name = 'User';
public $validate = array(
'email' => array(
'valid' => array(
'rule' => 'email',
'message' => 'Please enter an email address for username',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken',
),
'eValid' => array(
'required' => true,
'rule' => array('notEmpty'),
'message' => 'Please enter a username'
)
),
'password' => array(
'pValid' => array(
'required' => true,
'rule' => array('notEmpty'),
'message' => 'Please enter a valid password'
),
'minPword' => array(
'rule' => array('minLength', 8),
'message' => 'Please enter a password that is minimum 8 characters'
)
),
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this- >data[$this->alias]['password']);
}
return true;
}
}