您是否查看过flash data,它们可用于显示success、errors 和warnings 等是 或不是 的消息> 特定于您的模型属性。由您决定在哪里使用它们。
我主要在 transaction 块中插入数据或保存模型,当我保存多个模型时,我使用 try catch 块来获取发生的任何模型的错误,并使用会话闪存和 ArrayHelper 在其中添加错误闪现消息。
为了您的目的,您可以按以下方式使用它。
用
设置 flash 消息
Yii::$app->session->setFlash('error','you are not allowed to perform this message');
您可以在视图中使用以下内容获取它
if(Yii::$app->session->hasFlash('error')){
echo Yii::$app->session->gettFlash('error');
}
一个更复杂的方法是\kartik\widgets\AlertBlock
使用作曲家安装它
php composer.phar require kartik-v/yii2-widget-alert "*"
然后使用以下代码在layouts文件夹中创建一个名为alerts.php的文件
use kartik\widgets\AlertBlock;
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartik\widgets\Growl::TYPE_SUCCESS ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'success' )
] ,
]
] );
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartik\widgets\Growl::TYPE_INFO ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'info' )
] ,
]
] );
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartik\widgets\Growl::TYPE_DANGER ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'error' )
] ,
]
] );
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartik\widgets\Growl::TYPE_DANGER ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'danger' )
] ,
]
] );
AlertBlock::widget (
[
'useSessionFlash' => false ,
'type' => AlertBlock::TYPE_GROWL ,
'alertSettings' => [
'settings' => [
'type' => kartik\widgets\Growl::TYPE_WARNING ,
'icon' => 'glyphicon glyphicon-ok-sign' ,
'title' => 'Note' ,
'showSeparator' => true ,
'body' => Yii::$app->session->getFlash ( 'warning' )
] ,
]
] );
然后在$this->beginBody() 调用后将其包含在您的布局文件中,如下所示
<?= Yii::$app->view->renderFile ( '@frontend/views/layouts/alerts.php' ); ?>
那么您只需设置 Flash 消息,您甚至不必调用 getFlash(),扩展程序会自动显示它,您有以下可用变量
使用Yii::$app->session->setFlash('danger','You cannot do this');设置
编辑:
注意:每当您在设置 flash 消息后进行重定向时,请记住使用 return 和 redirect(),否则您可能会遇到消息未显示的问题。
return $this->redirect(['index']);
编辑 2:
您正在尝试为与当前保存的模型相关的任何特定模型添加错误消息,并且您想要一些允许您抛出异常并以格式良好的错误消息显示它们的灵魂,所以您的问题面临的是设置错误消息,如果我会这样做,我会使用以下方法。假设我有一个actionTest(),如下所示,它正在保存提交时的表单。
public function actionTest() {
$model = new Campaign();
if ($model->load(Yii::$app->request->post())) {
//start transaction block
$transaction = Yii::$app->db->beginTransaction();
try {
if (!$model->save()) {
//throw an exception if any errors
throw new \Exception(implode("<br />",\yii\helpers\ArrayHelper::getColumn($model->errors, 0,false)));
}
//commit the transaction if there arent any erorrs to save the record
$transaction->commit();
} catch (\Exception $ex) {
//roll back the transaction if any exception
$transaction->rollBack();
//catch the error and display with session flash
Yii::$app->session->setFlash('danger',$ex->getMessage());
}
}
$this->render('test');
}