在PhalconPHP的if($_POST)条件下无法使用flash消息


Unable to use flash messages within if($_POST) condition in PhalconPHP

我有这个在我的Signin控制器监听$_POST数据:

<?php
use 'Phalcon'Tag;
class SigninController extends BaseController {
    public function indexAction()
    {
        Tag::setTitle('Login');
        // if submit
        if ($_POST) {
            $user = Users::findFirst([
                "email = :email: AND password = :password:",
                "bind" => [
                    "email"    => $this->request->getPost('email'),
                    "password" => $this->request->getPost('password')
                ]
            ]);
            if ($user) {
                $this->session->set('id', $user->id);
                $this->session->set('role', $user->role);
                $this->response->redirect("account");
            } else {
                $this->flash->error('Wrong credentials!');
                $this->response->redirect('signin');
            }
        }
    }
}

但是当我提交表单时,没有显示带有"错误凭据"的flash消息。页面会重新加载。

我有这个在我的基础。volt模板:

<body>
    {{ flash.output() }}
    {% block content %}
    {% endblock %}
</body>

除了if($_POST)条件

在我的bootstrap文件中:

$di->set('flash', function() {
        $flash = new 'Phalcon'Flash'Session([
            'error'   => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice'  => 'alert alert-info',
            'warning' => 'alert alert-warning',
        ]);
        return $flash;
    });

是否知道为什么flash消息不工作在我的if($_POST)条件?

我被测试了!

1。引导文件

$di->set('flash', function(){
    return new Phalcon'Flash'Session(array(
        'error' => 'alert alert-error',
        'success' => 'alert alert-success',
        'notice' => 'alert alert-info',
    ));
});

控制:

$this->flash->error('Some Message');
$this->response->redirect('signin/index');

视图:

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

2。引导文件

$di->set('flash', function(){
        return new Phalcon'Flash'Direct(array(
            'error' => 'alert alert-error',
            'success' => 'alert alert-success',
            'notice' => 'alert alert-info',
        ));
    });

控制:

$this->flash->error('Jump to other page');
$this->dispatcher->forward(array('controller' => 'signin', 'action' => 'index'));

视图:

<?php 
    echo $this->getContent();
?>

我也遇到了同样的问题!

实际上,我们的问题与if ($_POST)语句没有任何关系。

是我们的观点导致了这个问题。要解决此问题,只需在设置flash消息之前禁用视图即可:

$this->view->disable();
$this->flash->error('Wrong credentials!');
$this->response->redirect('signin');

两种方法:使用$this->flash->error('Some Message');观点:

getContent ();?>

使用$this->flashSession->error('Some Message');观点:

flashSession ->输出()?>