在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'>×</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>
显示结果: