OK, in the src\Template\Layout\default.ctp view, I have this:
<?= $this->Flash->render() ?>
In the index function of a controller I set a flash message as:
$this->Flash->error((‘Flash error message.’);
OR
$this->Flash->error((‘Flash error message.’,[‘key’=>‘error’]));
Both works. The flash message is displayed on the view for the index function of the controller.
But, if I try to set the flash message in another function and the redirects the flash message is not displaying.
In the edit function of the same controller I have:
$this->Flash->error((‘Flash error message.’);
OR
$this->Flash->error((‘Flash error message.’, [‘key’=>‘error’]);
$this->redirect(array(‘action’=>‘index’));
Fails because once the user is redirected to the index page, previous code to flash message on the index function in the controller has been commented out by the way,
If I do not redirect the user in the edit function then the message is displayed.
My question is this… Does anyone knows why the flash message is not being displayed on the other page after a redirect? And do I have to set some sort of special parameters when calling the $this->redirect(array(‘action’=>‘index’)) so that the flash message is displayed on the directed page etc?
Thanks in advance for any help you may be able to give.
Normaly what I do is to display flash messages in the layout. Otherwise if you put it to the template and you forget to put it to all the templates, than it will not be shown.
Hi there, thanks for the reply. The message is already set to be displayed in the layout and is working. But when the flash message is set and followed by a redirect, then the message fails to display. Something rather strange must be happening.
I think what might be happening is that it displays the flash (thus clearing it from the session) and then redirects the user again.
It happened to me a while ago as well.
Please check how your app redirects your browser (this can be done using the network monitor of your browser).
What happens in CakePHP is:
$this->Flash->error(): Add an object to the array of "unrendered" flashes (in the session), telling CakePHP that this flash hasn't been rendered yet
$this->Flash->render(): Loop through the array of "unrendered" flashes, remove the object from the array and display the flash
Thanks FinlayDaG33k for the input. I have resolved the problem. The problem was being caused by a misconfiguration in the Session settings in App\config\app.php file. For some reason it did not like the way I had configured the “session.cookie_domain” settings.