'Error: Call to a member function allow() on a non-object' in CakePHP 3 AuthComponent
'Error: Call to a member function allow() on a non-object' in CakePHP 3 AuthComponent
根据 CakePHP 看起来有点混乱而且不太直接,我创建了一个基本的身份验证逻辑,但是,我似乎无法加载 Auth
组件。
这是来自 AppController.php
:
的代码部分
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => ['Form' => ['fields' => ['username' => 'email', 'password' => 'password']]],
'loginAction' => ['controller' => 'Users', 'action' => 'login'],
'loginRedirect' => ['controller' => 'Groups', 'action' => 'index'],
'logoutRedirect' => ['controller' => 'Users', 'action' => 'login']
]);
}
//Allow basic views
public function beforeFilter(Event $event)
{
$this->Auth->allow(['index', 'view', 'display']);
}
现在无论我 运行 使用哪个控制器或操作,我总是收到以下错误:
Error: Call to a member function allow() on a non-object
引用了以下行:
$this->Auth->allow(['index', 'view', 'display']);
它必须是直截了当的事情,但我在文档中找不到它,因此非常感谢任何帮助或指导。
检查您的子控制器的方法 initialize()
是否正在调用父方法。
class MyController extends AppController
{
public function initialize() {
parent::initialize();
//rest of code
}
}
我还没有创建 Template/Users/login.ctp
模板时就有了这个
只有在检查
获得的堆栈跟踪后才设法找出答案
$e = new \Exception('How did I got here anyway?');
debug($e->getTraceAsString());
屈服
#5 vendor/cakephp/cakephp/src/Error/ExceptionRenderer.php(318): Cake\Controller\Controller->render('missingTemplate')
根据 CakePHP 看起来有点混乱而且不太直接,我创建了一个基本的身份验证逻辑,但是,我似乎无法加载 Auth
组件。
这是来自 AppController.php
:
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => ['Form' => ['fields' => ['username' => 'email', 'password' => 'password']]],
'loginAction' => ['controller' => 'Users', 'action' => 'login'],
'loginRedirect' => ['controller' => 'Groups', 'action' => 'index'],
'logoutRedirect' => ['controller' => 'Users', 'action' => 'login']
]);
}
//Allow basic views
public function beforeFilter(Event $event)
{
$this->Auth->allow(['index', 'view', 'display']);
}
现在无论我 运行 使用哪个控制器或操作,我总是收到以下错误:
Error: Call to a member function allow() on a non-object
引用了以下行:
$this->Auth->allow(['index', 'view', 'display']);
它必须是直截了当的事情,但我在文档中找不到它,因此非常感谢任何帮助或指导。
检查您的子控制器的方法 initialize()
是否正在调用父方法。
class MyController extends AppController
{
public function initialize() {
parent::initialize();
//rest of code
}
}
我还没有创建 Template/Users/login.ctp
模板时就有了这个
只有在检查
获得的堆栈跟踪后才设法找出答案$e = new \Exception('How did I got here anyway?');
debug($e->getTraceAsString());
屈服
#5 vendor/cakephp/cakephp/src/Error/ExceptionRenderer.php(318): Cake\Controller\Controller->render('missingTemplate')