ProtectedController.php
<?php
class ProtectedController extends AppController {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'home', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'home', 'action' => 'index')
)
);
public $paginate = array(
'limit' => 2
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
AppController的
App::uses('Controller', 'Controller');
class AppController extends Controller {
}
UsersController
<?php
App::uses('ProtectedController', 'Controller');
class UsersController extends ProtectedController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'logout');
}
}
我一直在
Fatal Error
Error: Call to a member function allow() on a non-object
File: /Library/WebServer/Documents/cakephp_stats/app/Controller/ProtectedController.php
Line: 18
Notice: If you want to customize this error message, create app/View/Errors/fatal_error.ctp
现在出现错误。
有人如何解决这个问题。从我看到它应该加载ProtectedController组件和AuthComponent将被加载。
编辑:
第18行是ProtectedController:
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
编辑:
我现在唯一可以做的就是削减这个:
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'home', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'home', 'action' => 'index')
)
);
到AppController然后覆盖然后允许每个人:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'home', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'home', 'action' => 'index')
)
);
public function beforeFilter() {
$this->Auth->allow('*');
}
}
答案 0 :(得分:1)
我正在做类似的事情。与控制器的主要区别在于:
AppController
也包含Auth
组件,并在其$this->Auth->allow()
中执行了beforeFilter
。ProtectedController
未说明login / logoutRedirects,并调用其父beforeFilter
。ProtectedController
。我的受保护控制器只包含Auth
组件,并调用其父级beforeFilter
。只是推测:如果未经授权的人试图访问受保护资源,默认的Auth组件会重定向到/ users / login。当您保护UsersController并且不允许login
操作时,这可能会导致您遇到的行为。