首页 > 解决方案 > 如何使用引导程序将关闭图标添加到 phalcon 会话闪存消息

问题描述

我的服务配置如下;

$di->set('flashSession', function () {

 return new FlashSession([
        'error'   => 'alert alert-danger',
        'success' => 'alert alert-success',
        'notice'  => 'alert alert-info',
        'warning' => 'alert alert-warning'
    ]);
});

引导程序警报消息可以正常工作,但没有关闭图标。

如何使我的所有会话警报消息都具有关闭图标。

标签: phptwitter-bootstrapphalconflash-message

解决方案


这是会话闪存消息的快速消息。

在库目录中创建一个名为 BootstrapFlash.php 的文件

粘贴下面的代码

class BootstrapFlash extends \Phalcon\Flash\Session{

  public function __construct(){

    /* Pass html tags as receieved without reformating */

    parent::__construct();

    $this->setAutoescape(false);

  }

  public function message($type, $message){

    /* map the right class based on the message type */

    $types = array(
                      'success' => 'success',
                      'notice' => 'info',
                      'error' => 'danger',
                      'warning' => 'warning'
                    );

    $type = $types[$type];

    /* pretty title */

    $upper_type = strtoupper($type);

    /* Custom Alert Message with dismissible */

    $message = "<div class=\"alert alert-{$type} alert-dismissible\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button><strong>{$upper_type}!</strong> {$message}</div>";

    parent::message($type, $message);
  }
}

在 config 目录中的 service.php 文件中,如下设置您的 flash 会话;

$di->set('flashSession', function () {

    /* Custom file available in the library Directory */

    return new BootstrapFlash();
});

这样,您的 Flash 会话消息将具有关闭关闭按钮。

您也可以根据需要修改会话警报消息。


推荐阅读