Why doesn't Phalcon\Flash\Direct have an output() method?

Hi, just wondering why Phalcon\Flash\Direct doesn't have an output() method like Phalcon\Flash\Session has?


Because it doesn't need it, Phalcon\Flash\Direct outputs the flashed messages directly to the output so it doesn't need the session buffer as Phalcon\Flash\Session has implemented


@Phalcon But if i want to use Ajax, and i want to flash message indirectly. So what should i do? I have to write <div class="error alert"></div> myself.


@cnwggu You can change Phalcon\Flash\Direct to return the HTML instead of generate the output directly.

$flash = new Phalcon\Flash\Direct();

echo $flash->error("this is an error");


Digging month old topic but this is kind of relevant.

So basically, the only way of controlling the display location of flash messages in a volt template is to use Phalcon\Flash\Session? I suppose this could be done by overwriting the default 'flash' service with a new Phalcon\Flash\Session.


No, you need to place {{ content() }} where you want to show the output generated in the latest render level.


I must say, using Phalcon\Flash\Session instead of the direct one has so many benefits.

  • Easily customisable location (messages don't have to be in the last render level)
  • Messages work absolutely fine within a single page view
  • No need to worry about page forwards / redirections, messages will still come up after a 301 redirect

Might be worth dropping the entire concept of direct flash, as it seems a bit useless in its current form?


Phalcon\Flash\Session could be using a bit more of resources because it relies on the session, of course, the overhead is minimum. As you said, you can replace the default flash by Phalcon\Flash\Session if it seems more useful than default.


I agree that this is a useful feature. Phalcon\Flash\Direct::output() method will not be superfluous. For example I can use output in the main layout, not in a view.

Add a NFR for this discussion https://github.com/phalcon/cphalcon/issues/629

I know this question is very old but I would like to add something that may be helpful to those who may land here.

If you are using Phalcon\Flash\Direct and want to flash messages from Actions where you are redirecting, use Dispatcher foward and not response redirect in order to get the output. And not forgeting {{ content() }} as Phalcon mentioned.