Постоянное флеш-сообщение в cakephp версии 2.2.2 - PullRequest
0 голосов
/ 09 декабря 2018

На мой взгляд, у меня есть:

<?php echo $this->Session->flash();  ?>

В моем UsersController у меня есть:

public function login() {

        $this->Session->delete('Flash.auth');
        $this->Session->delete('Message.flash');
        $this->Session->delete('auth');
        $this->Session->delete('Message.auth');

        if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect(array('controller' => 'users', 'action' => 'index')));
        } else {
        $this->Session->setFlash('Invalid username or password, please try again', null, null);
            }
        }
public function logout() {

    $this->defaultTextView();

    return $this->redirect($this->Auth->redirect(array('controller' => 'users', 'action' => 'login')));
    }

function index($id = null) {    
        $this->Session->delete('Message.flash');
        $this->Session->destroy('Message.flash');
        $user_id_sess = $this->Session->read('Auth.User.id');
        $this->defaultAdminViewHere();
        $this->set('user_id_sess', $this->Session->read('Auth.User.id'));
        $this->User->id = $id;
        $this->set('user', $this->User->read());    
    }

Проблема в том, что мое флеш-сообщение сохраняется даже после успешного входа в систему.

pr ($ this-> Session-> read ());уступил:

Array
(
    [Config] => Array
        (
            [userAgent] => adcb84540c454620867cea3249a69ca1
            [time] => 1544388026
            [countdown] => 10
        )

    [Message] => Array
        (
            [flash] => Array
                (
                    [message] => Invalid username or password, please try again
                    [element] => default
                    [params] => Array
                        (
                        )
                )
        )
)

Вынужден добавить больше деталей, так как мой пост в основном кодовый.Не отображается сообщение об ошибке, если я использую неправильный пароль.После выхода из системы отображается сообщение об ошибке.

1 Ответ

0 голосов
/ 15 декабря 2018

Флэш-сообщение работает после минимального перенаправления, поэтому вы получаете это сообщение.При доступе к этому действию login для отображения формы входа flash message устанавливает, но не получает печать.После успешного входа в систему это старое флеш-сообщение печатается из-за одного перенаправления.Вам нужно написать свой метод входа в систему примерно так:

public function login() {

    if ($this->request->is('post')) {

        if ($this->Auth->login()) {

            $this->redirect($this->Auth->redirect(array('controller' => 'users', 'action' => 'index')));

        } else {

            $this->Session->setFlash('Invalid username or password, please try again', null, null);

            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }

}
...