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?