CakePHP customize flash message

谁说胖子不能爱 提交于 2019-12-03 03:46:47

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');

According to cakephp flash message no need to handle id and class in your view. You can set id and class when you setFlash as easy way.

$this->Session->setFlash(__('My message.'), 'default', array('id' => 'flashMessage', 'class' => 'message'), 'message');

$this->Session->setFlash(__('My message.'), 'default', array('class' => 'notification'), 'notification');

In your view.

echo $this->Session->flash('message');
echo $this->Session->flash('notification');

Thats it.

in app/element create .ctp file (example : flash.ctp)

<div class="alert alert-<?= (isset($alert)? $alert : 'info' ) ?>">
    <button type="button" class="close" data-dismiss="alert">×</button>
<center>    <?= (isset($message)? $message : 'Something went wrong' ) ?></center>
</div>

and use

$this->Session->setFlash($message,'flash',array('alert'=>'info'));

define class,type in your css file to customize your Flash Message I hope this will help

I don't think the current answers are a good approach to flash messages in general.

The entire application should simply have $this->setFlash('my message');and the parent view or layout should decide how to render it (even if it is by using an element).

The following code should be in the layout, perhaps in the <head> section.

<?php
        $flashMessage = $this->Session->flash();
        if ( $flashMessage !== false) {
            echo $this->element('my_custom_flash_element', array(
                'message' => $flashMessage
            ));
        }
?>

The layout captures it, and passes the flash message as a variable to the element 'my_custom_flash_element'.

Inside the element you can have all fancy rendering with css, and even cool stuff like toastr.js (which I personally love!)

Example: (my_custom_flash_element.ctp)

<script>
    $(document).ready(function() {
         <?php echo "toastr.warning('" . $message . "');"; ?>
    });
</script>

Also check out this wonderful concept on transient flash messages by @dereuromark

I found this workaround, using jquery.

I have this code in controller if($var1 == null) { $this->Flash->error(__('Error message')); }

In the /app/View/Layouts/default.ctp I put this code

$(document).ready(function() {  
    if($('#flashMessage').length) // Check if flashMessage exists
    {
       var message = $('#flashMessage').html(); // Copy text
       // if I send html tags in the flash message,I use this code to print html
       message = message.replace(/&lt;/g, '<'); 
       message = message.replace(/&gt;/g, '>');

        if( $('#flashMessage').hasClass("error") ) // Check if it has the error class
        {
            // Create a bootstrap alert warning and set the message
            $('#flashMessage').html("<div class='alert alert-warning' style='width:50%'>"+message+"</div>");
        }

        if( $('#flashMessage').hasClass("success") ) // Check if it has the sucess class
        {
            // Create a bootstrap alert sucess and set the message
            $('#flashMessage').html("<div class='alert alert-success' style='width:50%'>"+message+"</div>");
        }           
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!