Tuesday, January 18, 2022

[FIXED] CakePHP customize flash message

Issue

Normally, the $this->Session->setFlash(__('My message.'));

Will output:

<div id="flashMessage" class="message">
    My message.
</div>

How can I change that, so it'll output:

<p class="notification>
    My message.
</p>

instead?


Solution

If you look at the source code you will see that the second parameter of the SessionComponent method is the name of an element:

function setFlash($message, $element = 'default', $params = array(), $key = 'flash')

You can create a file in views/elements (or Views/Elements for Cake2) called for instance 'flash_notification.ctp'

with the following content:

<p class="notification">
  <?php echo $message; ?>
</p>

and use

$this->Session->setFlash(__('My message.'), 'flash_notification');


Answered By - nIcO

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.