【问题标题】:Cakephp 2.0 Logout with flash messageCakephp 2.0 使用 Flash 消息注销
【发布时间】:2014-05-23 18:30:33
【问题描述】:

我的注销功能运行良好,仅返回 $this->redirect($this->Auth->logout());.

但是我如何使用 Flash 消息创建注销功能?我试过这个功能。它不起作用。我总是登录,但它没有显示消息:(

public function logout() {
    if ($this->Auth->user('logged_in')) {
        $this->Session->setFlash(__('you are logout'), 'default', array('class' => 'alert alert-success'));
        return $this->redirect($this->Auth->logout());
    } else {
        $this->Session->setFlash(__('you aren\'t logout, Please, try again'), 'default', array('class' => 'alert alert-danger'));
    }
}

【问题讨论】:

  • $this->Auth->user('logged_in') 你为什么不通过 AuthComponent 使用官方和文档化的方法?这种自定义方法非常不常见并且可能很容易破坏。像 login() logout() 这样的东西也可能不喜欢它。请阅读有关如何正确操作的文档。

标签: php cakephp logout flash-message


【解决方案1】:

documentation 里面说,如果你想创建自定义消息,请在 /app/View/Elements 中执行。如果您想要您的个性化消息,那是不可行的。

在你的 /app/View/Elements/defaul.ctp

$this->Session->setFlash(__('you are logout'), 'default', array('class' => 'alert alert-success'));

在你的控制辊之后

public function logout() {
    if ($this->Auth->user('logged_in')) {
        $this->Session->setFlash('something', 'default');
        return $this->redirect($this->Auth->logout());
    } else {
        $this->Session->setFlash(__('you aren\'t logout, Please, try again'), 'default', array('class' => 'alert alert-danger'));
    }
}

您也可以通过component 进行操作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多