登录CakePhp 3

时间:2015-09-03 11:51:01

标签: cakephp-3.0

CakePhp 3在我尝试登录时出错 在非对象上调用成员函数check() 文件C:\ wamp \ www \ new \ admin \ src \ Controller \ UsersController.php

   namespace App\Controller;
//AppController.php
use Cake\Controller\Controller;
use Cake\Event\Event;

class AppController extends Controller
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', 
['fields'=> ['username' => 'username', 'password' => 'password'], 
'loginAction' => ['controller' => 'Users', 'action' => 'login'],  'loginRedirect' => ['controller' => 'IndexPage', 'action' => 'index'], 'logoutRedirect' => ['controller' => 'Users', 'action' => 'login']]);

}
}

//UsersController.php
public function login()
{
if($this->Session->check('Auth.user')){
$this->redirect(array('action' => 'index'));      
}

if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Invalid username or password'));
}
} 
}

1 个答案:

答案 0 :(得分:0)

使用$ this-> Auth-> identify();在controller login()函数中验证用户是否有效