CakePHP configure flash message

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

It will display:

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

How can I change this, so it will output:

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

instead

+5
source share
5 answers

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

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

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

with the following contents:

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

and use

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

According to the flash message, cakephp does not need to process the id and class in your view. You can set id and class when you install Flash as an easy way.

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

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

.

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

.

+3

/ .ctp(: 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>

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

, css, Flash- ,

0

, - - .

$this->setFlash('my message');, , ( ).

, , <head>.

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

- 'my_custom_flash_element'.

css , toastr.js( !)

: (my_custom_flash_element.ctp)

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

Also check out this great flash transition concept from @dereuromark

0
source

I found this workaround using jquery.

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

In / 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>");
        }           
    }
});
0
source

All Articles