我正在尝试实现此处的 ACL 教程: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
我遵循了所有说明,但是当我尝试转到 [my_site]/users/add 时,出现 ERR_TOO_MANY_REDIRECTS 错误。
我在 Cakephp 网站上找到了这个:
This happens when for example we have an element that receives data from a method of a controller, and the implementation of this method requires conducting the login would create an infinite loop that eventually will cause the browser to decline redirects
他们建议将此作为解决方案:
function beforeFilter () {
$ this -> Auth -> allow ( 'CONTROLLER_NAME' );
}
这似乎不起作用。
如果我更改 AppController:
public function beforeFilter() {
$this->Auth->allow('index', 'view', 'login', 'add');
}
至:
public function beforeFilter() {
$this->Auth->allow('*');
}
我不再收到错误消息,但被重定向到 [my_site]/users/login
对于我做错了什么导致我无法查看“用户添加”页面有什么建议吗? TIA!
用户 Controller :
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
}
登录功能(UsersController):
Public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
身份验证组件加载器:
public $components = array(
'Session',
'RequestHandler',
'Auth' => array(
'loginRedirect' => array('controller' => 'projects', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
)
);
最佳答案
您收到的错误与 ACL 无关,而是与 Auth 组件拒绝访问您的 UsersController
添加和登录函数有关,它试图将用户重定向到该函数。通过在您的 UsersController
(而不是您的 AppController
)中添加此行,确保 add
和 login
函数是公共(public)的):
public function beforeFilter() {
$this->Auth->allow(array('add', 'login'));
}
您现在遇到的循环是因为添加和登录函数不是公共(public)的,因此循环如下所示:添加 -> 未经授权 -> 登录 -> 未经授权 -> 登录
...和等等。
关于php - Cakephp 2 ACL ERR_TOO_MANY_REDIRECTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14561468/