CakePHP身份验证组件在注销后重定向到登录

CakePHP身份验证组件在注销后重定向到登录,cakephp,cookies,cakephp-2.0,Cakephp,Cookies,Cakephp 2.0,我刚开始使用CakePHP,但在设置Auth组件时遇到了问题每次用户注销时,cake都会将其重定向回登录状态。 您将看到我正在注销()期间尝试删除/取消设置一些cookie。这是因为我正在使用Wordpress站点设置单点登录,所以我希望用户同时从这两个站点注销 我的AppController: class AppController extends Controller { public $components = array( 'Session', 'Cookie'

我刚开始使用CakePHP,但在设置Auth组件时遇到了问题每次用户注销时,cake都会将其重定向回登录状态。 您将看到我正在注销()期间尝试删除/取消设置一些cookie。这是因为我正在使用Wordpress站点设置单点登录,所以我希望用户同时从这两个站点注销

我的AppController:

class AppController extends Controller {    

public $components = array(
    'Session',
    'Cookie',
    'Auth'      => array(
        'loginRedirect'     => array('controller' => 'questions', 'action' => 'index'), 
        'logoutRedirect'    => array('controller' => 'pages', 'action' => 'display', 'home')            
    )
);
public function beforeFilter() {
    $this->Auth->allow('index', 'view', 'login', 'logout', 'display');
}
}
class UsersController extends AppController {

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'logout', 'login');
}
public function beforeFilter(){
    parent::beforeFilter();

    $this->Auth->allow();
}
用户控制器:

class AppController extends Controller {    

public $components = array(
    'Session',
    'Cookie',
    'Auth'      => array(
        'loginRedirect'     => array('controller' => 'questions', 'action' => 'index'), 
        'logoutRedirect'    => array('controller' => 'pages', 'action' => 'display', 'home')            
    )
);
public function beforeFilter() {
    $this->Auth->allow('index', 'view', 'login', 'logout', 'display');
}
}
class UsersController extends AppController {

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'logout', 'login');
}
public function beforeFilter(){
    parent::beforeFilter();

    $this->Auth->allow();
}


如果没有登录,您似乎无法访问该页面。(您可以尝试访问URL,而无需登录以进行检查)

解决方案是在页面控制器中添加此
beforeFilter
函数:

class AppController extends Controller {    

public $components = array(
    'Session',
    'Cookie',
    'Auth'      => array(
        'loginRedirect'     => array('controller' => 'questions', 'action' => 'index'), 
        'logoutRedirect'    => array('controller' => 'pages', 'action' => 'display', 'home')            
    )
);
public function beforeFilter() {
    $this->Auth->allow('index', 'view', 'login', 'logout', 'display');
}
}
class UsersController extends AppController {

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'logout', 'login');
}
public function beforeFilter(){
    parent::beforeFilter();

    $this->Auth->allow();
}

PagesController将采用AppController的beforeFilter。我可能会这样做:

/**
 * GET /users/logout
 */
public function logout() {
    $this->Auth->logout();
    $this->redirect(array('controller' => 'pages', 'action' => 'display', 'home'));
}
注销时不需要允许,因为注销没有视图。您的应用程序控制器beforeFilter应如下所示:

public function beforeFilter() {
    $this->Auth->allow('index', 'view', 'display');
}
尝试使用:

return $this->redirect($this->Auth->logout());
在中找到该代码段:


不要忘记从beforeFilter中删除“Login”和“logOut”。

是因为您想在注销时重定向到其他页面吗?理想情况下,我会在注销后重定向,但我真的不在乎。现在,它会触发我的登录操作,这会导致用户即使在重定向之后仍保持登录状态。我完全搞不懂为什么登录操作应该参与注销过程(example.com/users/logout/),您是否尝试在
logout()函数中使用以下行?上次我在做一个测试项目。。。我只使用了这一行:
$this->redirect($this->Auth->logout())除非您特别需要上面那些与cookie相关的行…我确实需要cookie行,因为我必须同时注销Wordpress站点。我不确定这些线路是否与问题有关。为了安全起见,我会和他们一起测试。谢谢。我应该向页面控制器中的beforeFilter传递什么?另外,未登录的用户可以访问主页(注销会重定向到主页),这是否令人困惑?