我看了很多類似的問題,所以沒有人回答我的問題或能夠幫助我解決這個問題...基本上,當我註釋$ 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 –