Yii2:为什么在模块中渲染页面时setFlash不起作用?

I've an application using Yii2, basic template. In my app, I'm using Yii::$app->session-setFlash for show a message when render a page.

When I place my application in app, it work well. But when I move the app into module, it didn't show a message. The module called school

This is the code I've using for showing message and render page in my app/module/school

Yii::$app->session->setFlash('error', "Error!");
return Yii::$app->response->redirect(['school/student/create']);

the page successfully return to school/student/create page, but didn't show the message.

and this the code when I place the app in app

Yii::$app->session->setFlash('error', "Error!");
return Yii::$app->response->redirect(['create']);

the code above successfully return to page student/create and show the message.

This is my app directory structure:

--school
  --assets
  --commands
  --config 
  --controllers
  --file
  --mail
  --models
  --modules         //this the module
    --school
      --controllers
      --models
      --search
      --views
      --Module.php
  --runtime
  --test
  --vendor
  --views
  --web
  .....

Anyone know why It happen? and How do I can solve this?

Anyhelp will be appreciated, thanks :)

Edited:

This is code:

app/modules/school/views/student/_form.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\file\FileInput;

/* @var $this yii\web\View */
/* @var $model backend\models\Student */
/* @var $form yii\widgets\ActiveForm */
?>

<script type="text/javascript" src="../../web/js/jquery-1.5.2.min.js">                </script>

<div class="student-form">

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?=
$form->field($model, 'file')->widget(FileInput::classname(), [
    'options' => [
        'accept' => 'doc/*', 'file/*',

    ],
    'pluginOptions' => [
        'allowedFileExtensions' => ['csv', 'xls', 'xlsx'],
        'showUpload' => FALSE,
        'showPreview' => FALSE,
    ]
]);
?>
<?= Html::submitButton('<i class="glyphicon glyphicon-save-file"></i> UPLOAD', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary'], ['fakturout/create']) ?>
<?php ActiveForm::end(); ?>

</div>

and

app/modules/school/view/student/create.php

<?php
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model backend\models\Student */

$this->title = 'Student';
?>

<div class="title">
<?= Html::encode($this->title) ?>

</div>
<?=
  $this->render('_form', [
  'model' => $model,
])
?>
dotaer1993
dotaer1993 我需要那个吗?因为我的应用程序可以在app/views/student/create中显示消息/警报。
3 年多之前 回复
dongzi9196
dongzi9196 似乎在你的视图或布局中你没有适当的渲染警报小部件..我发布了一个简短的建议作为答案
3 年多之前 回复
dqm83011
dqm83011 你的意思是添加使用xxxxxxx;在学校/学生/create.php?
3 年多之前 回复
dst3605528
dst3605528 ,完成了。核实:)
3 年多之前 回复
duan00529
duan00529 是显示消息的页面(视图)的代码
3 年多之前 回复
doufuxing8691
doufuxing8691 你是指我显示消息的页面代码?
3 年多之前 回复
dongwen1909
dongwen1909 包含小部件,您将Flash消息渲染到学校/学生/创建视图
3 年多之前 回复
dongzhong5182163
dongzhong5182163 更新代码并显示您的查看代码..请..
3 年多之前 回复

1个回答

You have no line of code to render your session flash messages in the module. To test it add following code in the app/modules/school/view/student/create.php:

    <?php
    use yii\helpers\Html;

    /* @var $this yii\web\View */
    /* @var $model backend\models\Student */

    $this->title = 'Student';
    ?>

    <?php if (Yii::$app->session->hasFlash('success')): ?>
        <div class="alert alert-success">
            <?= Yii::$app->session->getFlash('success'); ?>
        </div>
    <?php endif; ?>
    <?php if (Yii::$app->session->hasFlash('warning')): ?>
        <div class="alert alert-warning">
            <?= Yii::$app->session->getFlash('warning'); ?>
        </div>
    <?php endif; ?>

    <?php if (Yii::$app->session->hasFlash('error')): ?>
        <div class="alert alert-danger">
            <?= Yii::$app->session->getFlash('error'); ?>
        </div>
    <?php endif; ?>

    <div class="title">
    <?= Html::encode($this->title) ?>

    </div>
    <?=
      $this->render('_form', [
      'model' => $model,
    ])
    ?>

I recommend add following code at the widget and render widget in your layout template (app/views/layouts/main.php), or add in layout template without widget:

<?php if (Yii::$app->session->hasFlash('success')): ?>
    <div class="alert alert-success">
        <?= Yii::$app->session->getFlash('success'); ?>
    </div>
<?php endif; ?>
<?php if (Yii::$app->session->hasFlash('warning')): ?>
    <div class="alert alert-warning">
        <?= Yii::$app->session->getFlash('warning'); ?>
    </div>
<?php endif; ?>
<?php if (Yii::$app->session->hasFlash('error')): ?>
    <div class="alert alert-danger">
        <?= Yii::$app->session->getFlash('error'); ?>
    </div>
<?php endif; ?>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐