Phalcon框架上扩展自定义Flash消息提示

本文介绍了如何在Phalcon框架中扩展并自定义Flash消息提示,包括如何使用Bootstrap样式以及添加关闭按钮。通过示例代码展示了实现方法,帮助开发者个性化消息提示框。
摘要由CSDN通过智能技术生成

在Phalcon框架上,消息提示实际上是使用Flash完成的,当然如果你愿意。

而默认的消息样式使用的bootstarp的样式结构,如果想自己定义,或者比如在消息提示框中加入关闭按钮,就可以按照如下的作法。


编码

bootstrap样式的消息提示框,只需要使用默认的样式,不需要另外设定

<?php
use Phalcon\Flash\Session as FlashSession;
class Session extends FlashSession
{
        public function __construct($cssClasses = null)
        {
                if ($cssClasses === null) {
                        $cssClasses = array(
                                'success' => 'alert alert-success',
                                'notice'  => 'alert alert-info',
                                'warning' => 'alert alert-warning',
                                'error'   => 'alert alert-danger',
                       );
                }
                parent::__construct($cssClasses);
        }
}

同样如果使用bootstrap的默认样式,并加入关闭按钮,就可以用如下的作法

<?php
class 
NewFlash extends Session
{
        public function __construct($cssClasses = null)
        {   
                if (is_array($cssClasses)) {
                        $cssClasses = array_map(function ($cssClass) {
                                return $cssClass.' fade in';
                        }, $cssClasses);
                }   
                parent::__construct($cssClasses);
        }   
        public function message($type, $message)
        {   
                $button = "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                parent::message($type, $button.$message);
        }   
}

然后注入 :

$di['flash'] = function () {   
     return new NewFlash();
}; 


或者:

$di->set('alaflash', function () {
	return new AlaFlash(array(
		'error'   => 'alert alert-danger',
		'success' => 'alert alert-success',
		'notice'  => 'alert alert-info',
		'warning' => 'alert alert-warning'
	));
});

使用时:

<?php
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
    public function indexAction()
    {
        $this->flash->success('Well done! You successfully read this important alert message. ');
        $this->flash->notice('Heads up! This alert needs your attention, but it\'s not super important. ');
        $this->flash->warning('Warning! Better check yourself, you\'re not looking too good. ');
        $this->flash->error('Oh snap! Change a few things up and try submitting again. ');
    }
}

显示样式:

<div class="container">
    {{ flash.output() }}
</div>

显示结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值