我看了很多类似的问题,所以没有人回答我的问题或能够帮助我解决这个问题...基本上,当我注释$ this-> auth-> allow行在NewsController中(因为我只希望通过身份验证的人访问除登录/注册以外的所有操作),它会导致登录无限循环。当我允许所有用户访问newscontroller中的索引操作时,它工作正常。任何想法为什么这将循环登录?CakePHP登录无限重定向
AppController的
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'news', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authorize' => array('Controller')
)
);
UsersController
<?php
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('register');
}
public function login() {
$this->layout = 'eprime_empty';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid username or password, try again', 'default', array('class' => 'warning'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
NewsController
<?php
class NewsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public function beforeFilter() {
parent::beforeFilter();
// $this->Auth->allow('index', 'view');
}
public function index() {
$this->set('news', $this->News->find('all'));
}
不要在控制器重新定义你的会话组件。您已经在应用程序控制器中声明了它。您正在覆盖该变量。我不知道这是否是你的问题,但你可以通过重新声明'public $ components = array('Session');' –
我已经从newscontroller中删除了该行(有道理),但是这样做可以排除AuthComponent不会影响我不幸的结果。 – medoix
我认为你在你的appcontroller中滥用''authorize'=>数组('Controller')'。如果你想在控制器的beforeFilter中使用动作授权,你必须将其设置为false。 'authorize'是用于控制器中的'isAuthorized'功能。 http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-no-authorization –