check if a Flash message exists in CakePHP 3

Spread the love

How can we check if a Flash message exists, whether in a view or in a controller?

In Yii (both version) for flash messages we have methods like: hasFlash for checking if a flash message exists beforehand and getFlash to get the content of a specific flash message…

In Yii 1x I would say something like this (whether in the Controller or in a View):



In CakePHP 3 this seems impossible, since the only thing I can do is to set a Flash message:

There are no get or has methods for Flash messages, or anything similar that I could find related to FlashComponent and FlashHelper classes.

The other thing about this Flash messages implementation in CakePHP 3 is the way you render and show them to the visitor; you just say the following in your views/layout:

That method will actually do the rendering, checking if a flash exists, whatever… and the thing is I need to check if a Flash message is sent/exists, then to do something else within the layout. Now it seems impossible to me or I was not looking in the right direction.

I would note that this is a very strange and bad implementation of Flash messages in CakePHP 3…

Finally, i found the solution :

we can use key while setting the Flash. see below

check by key in render before call

Leave a Reply

Your email address will not be published. Required fields are marked *