cakephp - auth注销重定向

时间:2012-09-17 15:50:43

标签: authentication redirect login cakephp-2.0 logout

在cakephp2.0中,我有 domain.com ,但当我退出(网址为 domain.com/logout )时,会重定向到域。 COM /应用程序/登录即可。我不希望它重定向到 domain.com/app/login ,而是重定向到 domain.com/logout 。这些是我在UsersController&我的AppController

class AppController extends Controller {
public $helpers = array ('Html', 'Form', 'Js' => array('Jquery'), 'Session');

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
         'authorize' => array('Controller') // Added this line
    )
);
}

Userscontroller

class UsersController extends AppController {
public $components = array(

    'Auth' => array(
        'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
         'authorize' => array('Controller') // Added this line
    )
);

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

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {

            $this->redirect('http://domain.com/thankyou');
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

public function logout() {
    $this->redirect($this->Auth->logout());
}
}

任何帮助都会很棒。感谢。

3 个答案:

答案 0 :(得分:5)

我最终在我的logout()函数中使用它

$this->Auth->logout()
$this->redirect(some url);

答案 1 :(得分:2)

您是否有注销页面的视图?您在注销后尝试显示的内容?可能发生的事情是用户已注销,但Cake无法显示注销页面,因为它是安全的,因此Cake会重定向到登录页面。

如果您启用了安全性并且想要向未登录的用户显示页面,则需要在其控制器中包含以下内容:

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

答案 2 :(得分:1)

你检查过了吗?

'Auth' => array(
    'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
    'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
)

并且

public function logout() {
    //$this->redirect($this->Auth->logout());
    //Change for :
    //I suppose that you have a logout.ctp view in your View/Pages
    $this->redirect(array('controller' => 'pages', 'action' => 'display', 'logout')
}

然后在你的rootes

Router::connect('/logout', array('controller' => 'pages', 'action' => 'display', 'logout'));

当然不要忘记

$this->Auth->allow('display'