我在 SO 上查看了很多类似的问题,但没有人回答我的问题或能够帮助我解决这个问题......基本上当我在 NewsController 中注释掉 $this->auth->allow 行时(因为我只有想要经过身份验证的人访问除登录/注册之外的所有操作)它会导致登录无限循环。当我允许所有用户访问新闻控制器中的索引操作时,它工作正常。任何想法为什么这会在登录时循环?
应用控制器
<?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')
)
);
用户控制器
<?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());
}
新闻控制器
<?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'));
}