该组件提供五种类型提示,总的样式有四种,提示类型有:error,danger,success,info,warning;样式有:alert-danger,lert-success,alert-info,alert-warning ;
使用方法:在控制器中加入:\Yii::$app->getSession()->setFlash('success', "头像设置成功!");
视图页面中引用:use app\components\widgets\Alert;
= Alert::widget() ?>
效果如图:
其实在控制器中没多大作用,就是在类中添加,可以很好做到提示作用;
具体 Alert.php 代码 如下:<?php
namespace app\components\widgets;
class Alert extends \yii\bootstrap\Widget
{
public $alertTypes = [
'error' => 'alert-danger',
'danger' => 'alert-danger',
'success' => 'alert-success',
'info' => 'alert-info',
'warning' => 'alert-warning'
];
public $closeButton = [];
public function init()
{
parent::init();
$session = \Yii::$app->getSession();
$flashes = $session->getAllFlashes();
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
foreach ($flashes as $type => $data) {
if (isset($this->alertTypes[$type])) {
$data = (array) $data;
foreach ($data as $message) {
/* initialize css class for each alert box */
$this->options['class'] = $this->alertTypes[$type] . $appendCss;
/* assign unique id to each alert box */
$this->options['id'] = $this->getId() . '-' . $type;
echo \yii\bootstrap\Alert::widget([
'body' => $message,
'closeButton' => $this->closeButton,
'options' => $this->options,
]);
}
$session->removeFlash($type);
}
}
}
}