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

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?



98.9k

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.



98.9k

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

$flash = new Phalcon\Flash\Direct();
$flash->setImplicitFlush(false);

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


15.1k

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.



98.9k

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



15.1k

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?



98.9k

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.



29.4k

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.

Thanks



812

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

$flash = new Phalcon\Flash\Direct();
$flash->setImplicitFlush(false);

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

works like a charm

Great article. We have an equally good article, please read:Combo Power Rack With Smith Machine Function Need a multifunction unit for full-body strength training? Yanre Fitness has the best power rack/smith machine combo for sale. Yanre is a self-design gym equipment manufacturer offering premium gym machines and fitness accessories for commercial use. We are proudly known as a leading supplier worldwide since 1997.