0

在 cakephp2.0 中,我有domain.com但是当我注销时(url 是domain.com/logout),它被重定向到domain.com/app/login。我不希望它重定向到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
    )
);
}

用户控制器

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());
}
}

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

4

3 回答 3

7

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

$this->Auth->logout();
$this->redirect(some url);
于 2012-09-19T19:11:09.667 回答
2

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

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

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('login','logout');
}
于 2012-09-17T21:18:21.463 回答
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')
}

然后在你的根

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

当然不要忘记

$this->Auth->allow('display'
于 2012-09-18T23:48:51.190 回答