如何检查cakephp中的flash消息集是否为空

How to check if the flash message set is empty in cakephp

我有一个登录表单,我在控制器中设置了无效登录时的闪现消息:

$this->Session->setFlash(__('Invalid username or password'));

并在 ctp 文件中显示如下消息:

<div id="error" >
<?php
echo $this->Session->flash();
?> 
</div>

CSS为div如下:

    #error {
    padding:10px;
    display:inline-block;
    margin-right: 18px;
    font-size:16px;
    font-weight:600;
    position:relative;
    top:20px;
    }

加载页面时,会显示一条空的闪现消息。 如何检查$this->Session->flash();是否为空并仅在不为空时显示div?

当消息为空时,页面加载时的闪现消息如下所示:

消息设置后的类似方式:

试试这个:

使用错误警报成功警报等创建您的自定义元素...

/View/Elements/error_alert.ctp

<!-- Example Bootstrap Error Alert -->
<div class="alert alert-danger">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong><?php echo $message ?></strong>
</div>

/View/Elements/success_alert.ctp

<!-- Example Bootstrap Succes Alert -->
<div class="alert alert-success">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong><?php echo $message ?></strong>
</div>

在您的 Views 添加这个,它只在必须显示消息时出现

 <!-- It´s will work for all alert types (not only for error alerts) -->
<div class="col-md-12">
    <?php echo $this->Session->flash(); ?> 
</div>

如果您想使用一个,请在您的控制器中添加:

成功提示

$this->Session->setFlash('Success message!', 'success_alert');

错误提示

$this->Session->setFlash('Error message!', 'error_alert');