Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Flash messages not working inside if($_POST) condition within the same controller

I don't know if it's a bug or not, but it drives me crazy.

Firstly, this is what I have in my bootstrap (I am using session for storing flash messages):

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

and this is my output in the view:

<body>
    {{ flash.output() }}

    {% block content %}

    {% endblock %}
</body>

Secondly, this is working inside my controller and flash messages (in my form I am pointing action to signin/doSignin) :

<?php

class SigninController extends BaseController {

    public function indexAction()
    {
    }

    public function doSigninAction()
    {
        $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");
        } 

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

   }

}

However, if I want to perform the if ($_POST) check within the same indexAction method (form's action is set to action="signin" I have a problem and it stops working and no flash messages will appear. e.g. this is not working:

<?php

class SigninController extends BaseController {

    public function indexAction()
    {

        if ( $this->request->isPost() ) {

            $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");
            } 
            $this->flash->error('Wrong credentials!');
            $this->response->redirect('signin');

        }

    }

}

It just redirect and reload the user if the email and password is wrong, but no flash message is being shown.

Why is that? Any idea how I can be able to use flash messages from within if($_POST) or if( $this->request->isPost() ) conditions inside the same method indexAction?



23.3k
Accepted
answer
edited Jul '14

SOLVED We need to stop generating the views like that:

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

And then it works even from inside the if($_POST) or if( $this->request->isPost() ) onditions within the same method too. I hope this will help somebody in the future ;)



174
edited Jul '14

Order in boostrap is important!

$di->set('session', function() use ($config) {
    $session = new \Phalcon\Session\Adapter\Files();
    $session->start();
    return $session;
}, true);
$di->set('flash', function() {
    return new \Phalcon\Flash\Session([
        'error'         => 'alert alert-danger',
        'notice'        => 'alert alert-info',
        'success'       => 'alert alert-success',
        'warning'       => 'alert alert-warning',
    ]);
});

My login action in UsersController:

public function loginAction()
{
    if ($this->request->isPost() && $this->security->checkToken()) {
        if ($user = Users::authenticate($this->request->getPost())) {
            $this->session->set('user', (object) [
                'id' => $user->id,
                'username' => $user->username,
            ]);
        $this->flash->success(_('You have successfully logged on.'));
        return $this->response->redirect('users/profile');
        }
        $this->flash->error(_('Bad credentials.'));
    }
} // Users:authenticate is defined in Model.

My volt index layout:

{{ flash.output() }}
{{ content() }}

All works fine.

Best regards --cesar



23.3k

Thanks ceasar. I have session before flash in my bootstrap as you all the time, so that's not the problem.

What version of Phalcon are you using? I am using 1.3.2.

i have noticed that this version I am using has some bugs, e.g. the security hash is not working on windows check here: https://github.com/phalcon/cphalcon/issues/2277



174

I am working on Slackware Linux 64 bits current branch

  • Apache 2.4.9
  • PHP 5.4.29
  • Phalcon 1.3.2

security->hash not working properly on PHP < 5.5. I use sha1 as password hash method although in the future I will use bcrypt.