How to set flash in CAKEPHP view?

1

In the method on my controller I will flash the box, according to the condition.

if ($this->Auth->user()) {  
//redireciona apos o login;  

echo $this->Session->setFlash("Bem-vindo");  
} 

How can I display this directly from the view, instead of displaying it in the method? For example, the method would return true in case of logging and on the page to which I redirected the user I would access the flash message or something.

I do not want to use echo , as it is a short and temporary message or until the page is reloaded or refreshed.

I have tried to access $this->Session->setFlash() , but it gives helper error. Maybe, would I creating a helper solve the problem then? $ this->Session->flash() has no message. You would only access a component, in this case it could be Auth , for example: $this->Session->flash('Auth');

Does anyone suggest anything?

    
asked by anonymous 17.10.2015 / 15:19

2 answers

2

I do not know which version of CakePHP you are using, but in 1.2 just use the $ session helper and call the flash () method without the echo

<html>
<body>
<?php
if ($session -> check('Message.flash')) {
  $session -> flash();
}
?>
<div>sua pagina normal</div>
</body>

Link: link

    
27.10.2015 / 15:02
0

As you did not specify in which version, I'm completing the Claytinho response

In CakePHP 2.x

// In the view.
echo $this->Session->flash();

In CakePHP 3.x

// In your Controller
$this->Flash->success('The user has been saved', [
    'key' => 'positive',
    'params' => [
        'name' => $user->name,
        'email' => $user->email
    ]
]);

// In your View
<?= $this->Flash->render('positive') ?>

<!-- In src/Template/Element/Flash/success.ctp -->
<div id="flash-<?= h($key) ?>" class="message-info success">
    <?= h($message) ?>: <?= h($params['name']) ?>, <?= h($params['email']) ?>.
</div>
    
27.10.2015 / 15:31