2018-04-26 12:22
浏览 37

如何检查CakePHP 3中是否存在Flash消息?

In Yii (both version) for flash messages we have methods like: hasFlash for checking if a flash message exists beforehand and getFlash to get the content of a specific flash message...

In Yii 1x I would say something like this (whether in the Controller or in a View):


<?php if(Yii::app()->user->hasFlash('success')): ?>
    <div class="alert alert-success">
        <?=Yii::app()->user->getFlash('success'); ?>
<?php endif; ?>


if($something->happend()) {
    Yii::app()->user->setFlash('success', 'You\'ve done something wonderful.');
    return $this->redirect('/elsewhere');

In CakePHP 3 this seems impossible, since the only thing I can do is to set a Flash message:

$this->Flash->set('Welcome, to the real world.', [
    'element' => 'success',

There are no get or has methods for Flash messages, or anything similar that I could find related to FlashComponent and FlashHelper classes.

The other thing about this Flash messages implementation in CakePHP 3 is the way you render and show them to the visitor; you just say the following in your views/layout:

<?= $this->Flash->render(); ?>

That method will actually do the rendering, checking if a flash exists, whatever... and the thing is I need to check if a Flash message is sent/exists, then to do something else within the layout. Now it seems impossible to me or I am not looking in the right direction.

I would note that this is a very strange and bad implementation of Flash messages in CakePHP 3...

Finally, the question:

How can I check if a Flash message exists, whether in a view or in a controller?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanlaiyin2356 2018-04-26 12:52

    You can use key while setting the Flash. see below

    $this->Flash->set('Welcome, to the real world.', ['key' => 'alert']);

    check by key in render before call

        echo $this->Flash->render('alert');
    打赏 评论
  • douyanpeng0748 2018-04-26 12:44

    The documentation on Setting Flash Messages says that the messages are stored in the session. More specifically it says:

    FlashComponent’s __call() and set() methods optionally take a second parameter, an array of options:

    • key Defaults to ‘flash’. The array key found under the Flash key in the session.

    So to check if a flash message exists you would do:


    Or if you specified a key when setting the flash message you would do:

    打赏 评论
  • duanbai5348 2018-04-26 13:51


    It returns true/false depending on whether there are any such flash messages set. ->read() does the same thing, but returns the flash data if there is (any and crucially, it leaves the session var so it can still be echoed later).

    打赏 评论
  • dongxuandong2045 2018-12-26 04:13

    One ugly and quick way to do this is:

    <?php $flashRender = $this->Flash->render(); ?>
    <?php if(!empty($flashRender)) : ?>
          <?= $flashRender ?>
    <?php endif; ?>

    Only render the flash when it is set

    打赏 评论

相关推荐 更多相似问题