We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

FlashSession cleared after call to flashSession.getMessages(type)

Hi, i have updated phalcon from 1.3.x to 2.0.9 and I am seeing some unexpected behaviour in the use of FlashSession->getMessages() and I am not sure if it is a bug or normal behaviour.

I'm adding flashmessages with \Phalcon\Flash\Session

$this->flashSession->error('oops something is wrong');

and in my volt i have these for loops to retrieve each message per type. This was working fine with 1.3.x

{% for m in flashSession.getMessages('success') %}
<div class="alert alert-success">{{ m }}</div>
{% endfor %}
{% for m in flashSession.getMessages('error') %}
<div class="alert alert-error">{{ m }}</div>
{% endfor %}

This doesnt work anymore. Only the first loop is working. If i add a flashmessage with success('hello'); it is showing but with error(' bye'); it is not showing. So I took a look in the code and it looks like all sessions of the flash messages are being removed instead of only the messages of the type of the first parameter if you call getMessages(type).

the method _getSessionMessages(boolean remove, type = null) that is being called by \Phalcon\Flash\Session::getMessages() ends with:

if(remove) { session->remove('_flashMessages') }

https://github.com/phalcon/cphalcon/blob/2.0.x/phalcon/flash/session.zep

Is this the way it should work or is it a bug?

ps i have no clue how i am suppose to use coding blocks here... the suggested way with backticks is messing up my message?



125.8k
Accepted
answer

Hi @kaphert,

I've updated your post with proper backticks & highlighting - [ Edit ] it to see the changes I made. You need a full blank line around code blocks or it doesn't get interpreted.

As for your actual question: Looking at the documentation, it would appear all you need to do is pass FALSE as a second argument to flashSession.getMessages() in order to get the messages to persist.



21.2k

Hi Dylan, thank you for your reply. Yes that seems to be a solution, but isnt it strange behaviour that if you ask for messages of a specific type it removes all messages instead of only the messages of that type by default?

Yeah, I would call that deletion a "side-effect". To be honest, the fact that getMessages() does any deletion at all is a side-effect. Personally, I wrote my own Feedback component rather than using the built-in one.



21.2k

Thank you dylan for your help. I will use the option with the second paramater 'false' for now.



21.2k

This problem is solved in the new 2.0.10 release with commit: https://github.com/phalcon/cphalcon/pull/11060 so the behaviour is like you expect now.

getMessages($type = 'success',$remove = true) only removes all flashsessions from the type 'success' not all other flash sessions