Yii::$app->session->setFlash() is not working

Hi guys,

following code in SiteController works fine with echo, but not with yii-method setflash(). Maybe, I have to reconfigure my config-file(main-local.php)?

Any other ideas how to keep setflash() doing its job?




    public function actionScript() { //A new method, programmed by Thomas Kipp

        $model = new myScriptForm();

        $uploadPath = Yii::getAlias('@uploading');


        if (isset($_FILES[$fileName])) {

            $file = \yii\web\UploadedFile::getInstanceByName($fileName);


            if ($file->saveAs($uploadPath . '/' . $file->name)) {

                echo"<script>alert('Hallo');</script>";

                //echo \yii\helpers\Json::encode($file);

            }

        }

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->avatar = UploadedFile::getInstances($model, 'avatar');

            if ($model->avatar) {

                $counter = 0;

                foreach ($model->avatar as $avatar) {

                    echo "<font size='4'><br><center>File <font color='red'> "

                    . "$avatar<font color='black'> successfully uploaded."

                    . "<br>It's available in folder 'uploadedfiles' </font></font color></center>";

                    $avatar->saveAs(Yii::getAlias('@uploadedfilesdir/' . $avatar->baseName . $counter . '.' . $avatar->extension));

                    $counter++;

                    Yii::$app->session->setFlash('success', 'Avatar has been uploaded');

                }

            } else

                echo"<font size='4'><br><center>No Upload-file selected.<br>"

                . "Nothing moved into folder 'uploadedfiles' </font></center>";

            Yii::$app->session->setFlash('error', 'There has nothing to be uploaded');

            return $this->render('myScript', ['model' => $model]);

        }

        else {

            return $this->render('myScript_Formular', ['model' => $model]);

        }

    }



Do you have some widget on the template to show the flash messages?

On your layout you need to have a <?= Alert::widget() ?> and use for example this widget -> https://github.com/yiisoft/yii2-app-advanced/blob/master/common/widgets/Alert.php

I didn’t use any widget since now managing my messages. 'cause setflash() isn’t doing it’s job, I decided to use Growl-Wiget of Kartik in order to present my messages. Nonetheless, it’s strange, 'cause only a virginally installed version of yii2 shows messages using setflash()-method. Here is my JSON-File




{

    "name": "yiisoft/yii2-app-advanced",

    "description": "Yii 2 Advanced Project Template",

    "keywords": ["yii2", "framework", "advanced", "project template"],

    "homepage": "http://www.yiiframework.com/",

    "type": "project",

    "license": "BSD-3-Clause",

    "support": {

        "issues": "https://github.com/yiisoft/yii2/issues?state=open",

        "forum": "http://www.yiiframework.com/forum/",

        "wiki": "http://www.yiiframework.com/wiki/",

        "irc": "irc://irc.freenode.net/yii",

        "source": "https://github.com/yiisoft/yii2"

    },

    "minimum-stability": "stable",

    "require": {

        "php": ">=5.4.0",

        "yiisoft/yii2": "~2.0.6",

        "yiisoft/yii2-bootstrap": "~2.0.0",

        "yiisoft/yii2-swiftmailer": "^2.0",

        "kartik-v/yii2-widget-datetimepicker": "*",

        "kartik-v/yii2-sortable": "*",

        "kartik-v/yii2-widget-touchspin": "*",

        "kartik-v/yii2-widgets": "*",

        "kartik-v/yii2-icons": "dev-master",

        "yiisoft/yii2-debug": "^2.0",

        "warrence/yii2-kartikgii": "dev-master"

    },

    "require-dev": {

        "yiisoft/yii2-gii": "~2.0.0",

        "yiisoft/yii2-faker": "~2.0.0",


        "codeception/base": "^2.2.3",

        "codeception/verify": "~0.3.1"

    },

    "config": {

        "process-timeout": 1800,

        "fxp-asset":{

            "installer-paths": {

                "npm-asset-library": "vendor/npm",

                "bower-asset-library": "vendor/bower"

            }

        }

    }

}



I don’t understand exactly what are you talking about.

But to use setFlash to show a message you will need some code on your views to read the flash message and show it, usually you can use this widget https://github.com/yiisoft/yii2-app-advanced/blob/master/common/widgets/Alert.php and add it on your layout. This is really simples and works every time.

What code do you have on the layout/view side to show the flash messages that you set via “Yii::$app->session->setFlash(‘success’, ‘Avatar has been uploaded’);”

in my layout I have:




    use common\widgets\Alert;

    ...

    <?= Alert::widget() ?>

    <?= $content ?>



and it works fine

I solved problem like this:




  public function actionScript() { //A new method, programmed by Thomas Kipp

        $model = new myScriptForm();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->fileImage = UploadedFile::getInstance($model, 'fileImage');

            $model->avatar = UploadedFile::getInstance($model, 'avatar');

            if ($model->fileImage) {

                echo Growl::widget([

                    'type' => Growl::TYPE_SUCCESS,

                    'title' => 'Upload Succeeded',

                    'icon' => 'glyphicon glyphicon-info-sign',

                    'body' => 'Uploadfile(s) succesfully have been uploaded<br>It is in folder uploadedfilesdir',

                    'showSeparator' => true,

                    'delay' => false,

                    'pluginOptions' => [

                        'showProgressbar' => false,

                        'placement' => [

                            'from' => 'top',

                            'align' => 'center',

                        ]

                    ]

                ]);


                $model->fileImage->saveAs(Yii::getAlias('@uploadedfilesdir/' . $model->fileImage->baseName . '.' . $model->fileImage->extension));

            } else {

                echo Growl::widget([

                    'type' => Growl::TYPE_GROWL,

                    'title' => 'No Uploads!',

                    'icon' => 'glyphicon glyphicon-exclamation-sign',

                    'body' => 'No uploadfile chosen.<br>So,nothing has been uploaded',

                    'showSeparator' => true,

                    'delay' => false,

                    'pluginOptions' => [

                        'showProgressbar' => false,

                        'placement' => [

                            'from' => 'top',

                            'align' => 'center',

                        ]

                    ]

                ]);

            }

            if ($model->avatar) {

                echo Growl::widget([

                    'type' => Growl::TYPE_INFO,

                    'title' => 'Upload succeeded',

                    'icon' => 'glyphicon glyphicon-info-sign',

                    'body' => 'Avatar(s) succesfully have been uploaded.<br>It is in folder uploadedfilesdir',

                    'showSeparator' => true,

                    'delay' => false,

                    'pluginOptions' => [

                        'showProgressbar' => false,

                        'placement' => [

                            'from' => 'top',

                            'align' => 'center',

                        ]

                    ]

                ]);

                $model->avatar->saveAs(Yii::getAlias('@uploadedfilesdir/' . $model->avatar->baseName . '.' . $model->avatar->extension));

            } else {

                echo Growl::widget([

                    'type' => Growl::TYPE_DANGER,

                    'title' => 'Oh snap!No Uploads!',

                    'icon' => 'glyphicon glyphicon-remove-sign',

                    'body' => 'No uploadfile chosen.<br>So,nothing has been uploaded',

                    'showSeparator' => true,

                    'delay' => false,

                    'pluginOptions' => [

                        'showProgressbar' => false,

                        'placement' => [

                            'from' => 'top',

                            'align' => 'center',

                        ]

                    ]

                ]);

            }

            return $this->render('myScript', ['model' => $model]);

        } else {

            return $this->render('myScript_Formular', ['model' => $model]);

        }

    }



Glad you found a way. And Happy you posted solution.