1

I have this in my Signin controller to listen for a $_POST data:

<?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');
            }

        }

    }

}

But the flash message with "Wrong credentials" is not being shown when I submit the form. The page just reloads.

I have this in my base.volt template:

<body>

    {{ flash.output() }}

    {% block content %}

    {% endblock %}

</body>

and it's working for everything except if($_POST) condition.

I have this in my bootstrap file:

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

Any idea why the flash messages do not working inside my if($_POST) condition?

user3797244
  • 53
  • 1
  • 5
  • 1
    You should use flashSession with redirect(), not flash. – Phantom Jul 02 '14 at 12:32
  • I have this in my bootstrap file: `$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; });` – user3797244 Jul 02 '14 at 12:37
  • @Phantom Any idea why it's not working? – user3797244 Jul 02 '14 at 13:16
  • Try to replace redirect with internal forward: `$this->dispatcher->forward(array('controller' => 'controller_name', 'action' => 'action_name'));` – Phantom Jul 02 '14 at 14:34

3 Answers3

0

Tow way I am Tested!

1.bootstrap file

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

Control:

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

View:

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

2.bootstrap file

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

Control:

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

View:

<?php 
    echo $this->getContent();
?>
ikool
  • 153
  • 1
  • 9
0

I had the same problem!

In fact, our problem does not have anything to do with our if ($_POST) statement.

It is our view that causes this problem. To solve this issue, it is as easy as disabling the view before setting the flash message:

$this->view->disable();
$this->flash->error('Wrong credentials!');
$this->response->redirect('signin');
Hesam
  • 146
  • 1
  • 6
0

two method: use $this->flash->error('Some Message'); View:

getContent(); ?>

use $this->flashSession->error('Some Message'); View:

flashSession->output() ?>