douxin1163 2017-02-04 12:14
浏览 156

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,
])
?>
  • 写回答

1条回答 默认 最新

  • dongzhang7157 2017-02-07 03:47
    关注

    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; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!