我正在学习CakePHP。在控制器中,我编写了一个这样的程序。我还在登录页面上创建了一个表单。但它显示$this->Auth->login();.
如果我在CakePHP中这样给出它,它会显示这个错误:
Error: Call to a member function login() on a non-object
File: C:\wamp\www\cakephp\app\Controller\UsersController.php
Line: 20
public function login() {
$this->Auth->authenticate = array('Form');
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password. Try again.'));
}
}
答案 0 :(得分:1)
将此添加到您的AppController.php
class AppController extends Controller {
public $components = array(
'Auth' => array(
'loginAction' => array('controller'=>'User', 'action'=>'login'),
'loginRedirect' => array('contoller'=>'User', 'action'=>'login'),
'logoutRedirect' => array('controller'=>'User', 'action'=>'logout'),
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array(
'username' => 'email',
'password'=>'password'
)))));
这在UserController.php
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('login');
}
这在app/Model/User.php
public function beforeSave($options = array()) {
parent::beforeSave($options = array());
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}