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'));
}
}
}
答案 0 :(得分:0)
使用$ this-> Auth-> identify();在controller login()函数中验证用户是否有效