1

我在我的登录控制器中有这个来监听 $_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 消息。页面只是重新加载。

我的 base.volt 模板中有这个:

<body>

    {{ flash.output() }}

    {% block content %}

    {% endblock %}

</body>

它适用于除 if($_POST) 条件之外的所有内容。

我的引导文件中有这个:

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

知道为什么闪存消息在我的 if($_POST) 条件下不起作用吗?

4

3 回答 3

0

我有同样的问题!

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

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

$this->view->disable();
$this->flash->error('Wrong credentials!');
$this->response->redirect('signin');
于 2015-01-08T19:33:50.147 回答
0

我被测试了!

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();
?>
于 2014-07-03T09:27:34.187 回答
0

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

获取内容();?>

使用 $this->flashSession->error('Some Message'); 看法:

flashSession->输出()?>
于 2016-03-16T09:44:46.963 回答