Yii2 setflash error

Set your messages in a controller:
  1. Summary
  2. Setting flash messages
  3. Displaying flash messages
  4. Links

Summary

Set your messages in a controller:

Yii::app()->user->setFlash('success', "Data1 saved!");
Yii::app()->user->setFlash('error', "Data2 failed!");
Yii::app()->user->setFlash('notice', "Data3 ignored.");

Display them in your view:

<?php
    foreach(Yii::app()->user->getFlashes() as $key => $message) {
        echo '<div class="flash-' . $key . '">' . $message . "</div>n";
    }
?>

Setting flash messages

A flash message is used in order to keep a message in session through one or several requests of the same user. By default, it is removed from session after it has been displayed to the user. Flash messages are usually used in combination with HTTP redirections, because in this case there is no view, so messages can only be displayed in the request that follows redirection.

A flash message has a name and a content (AKA key and value). It is an entry of an associative array. The name is a string: often «notice», «success», or «error», but it can be anything. The content is usually a string. You can put HTML tags in your message if you display it raw. You can also set the message value to a number or an array: it will be serialized and kept in session like a string.

Flash messages can be set using the setFlash() Method of [CWebUser]. For example, if you would like to inform the user that his changes were successfully saved, you could add the following line to your Controller:

<?php
Yii::app()->user->setFlash('success', "Data saved!");
$this->redirect(array('thing/view', 'id' => 1));

In this example we used the key ‘success’. If you want to define more than one flash messages, you will have to use different keys.

Displaying flash messages

To check for flash messages we use the hasFlash() Method and to obtain the flash message we use the getFlash() Method. Since Yii v1.1.3, there is also a method getFlashes() to fetch all the messages.

By default, fetching a message deletes it from the session. This means that a message is meant to be displayed only on the first page served to the user. The fetching methods have a boolean parameter that can change this behavior. See the API links in the previous paragraph.

Displaying statically

So showing of the flash message defined above in a view is done by

<?php if(Yii::app()->user->hasFlash('success')):?>
    <div class="info">
        <?php echo Yii::app()->user->getFlash('success'); ?>
    </div>
<?php endif; ?>

These few lines of code will make a flash message with the key «success» visible to the user within a div of class «info». The message will be displayed until this or another page is (re)loaded in the browser.

If you want to always display all the flash messages, then you should add a block to your layout (by default protected/views/layout/main.php). Here is a more elaborate example:

<?php
$flashMessages = Yii::app()->user->getFlashes();
if ($flashMessages) {
    echo '<ul class="flashes">';
    foreach($flashMessages as $key => $message) {
        echo '<li><div class="flash-' . $key . '">' . $message . "</div></li>n";
    }
    echo '</ul>';
}
?>

The default CSS created by the Yii script yiic webapp has directives for three classes of flash messages on a div tag: flash-error, flash-notice, flash-success.

The best way to know if some flash messages are set is to check if Yii::app()->user->getFlashes() is empty. Since v1.1.7, Yii keeps an associative array of the flash keys in the form array("key1" => 0, ...), or null if not flash message is set. You can fetch this with Yii::app()->user->getState(CWebUser::FLASH_COUNTERS) but this is not recommended, as Yii could change this internal process.

Displaying dynamically (with Javascript)

If you want the flash message to appear somewhere above the content and then automatically fade out after a few seconds, you will have to add the following lines to your view:

<?php
Yii::app()->clientScript->registerScript(
   'myHideEffect',
   '$(".info").animate({opacity: 1.0}, 3000).fadeOut("slow");',
   CClientScript::POS_READY
);
?>

With these lines of code we register a piece of jQuery (already included with YII) javascript code, using ‘myHideEffect’ as ID. It will be inserted in the jQuery’s ready function (CClientScript::POS_READY). Due to the chainablity of jQuery the little script will run two effects on the .info DIV sequentially:

[javascript]
    .animate({opacity: 1.0}, 3000)

Normally this would animate the .info DIV to a full opacity within 3 seconds. But the DIV is already rendered with full opacity upon page load, so calling this effect will just cause a delay for 3 seconds.

[javascript]
    .fadeOut("slow")

This is the fadeOut effect which will hide the .info DIV at slow speed.

Further readings (JS):
  • More information about the jQuery effects can be found here.
  • Using CJuiDialog to display flash Messages in Dialogues

Links

  • CWebUser API
  • A more specific use of flash messages: An easy way to display a success page using flash messages

Setting flash message

A flash message is used in order to keep a message in session through one or several requests of the same user. By default, it is removed from session after it has been displayed to the user.

Flash messages can be set using the setFlash() Method

Add below code in your controller file like:

Yii::$app->session->setFlash('success', "Your message to display.");

Example:

class ProductsController extends yiiwebController
{
    public function actionCreate()
    {
         $model = new User();

         if ($model->load(Yii::$app->request->post())) {
              if ($model->save()) {
                  Yii::$app->session->setFlash('success', "User created successfully."); 
              } else {
                  Yii::$app->session->setFlash('error', "User not saved.");
              }
              return $this->redirect(['index']);
         }
         return $this->render('create', [
             'model' => $model
         ]);
    }
}

Displaying flash message

To check for flash messages we use the hasFlash() Method and to obtain the flash message we use the getFlash() Method.

By default, fetching a message deletes it from the session. This means that a message is meant to be displayed only on the first page served to the user. The fetching methods have a boolean parameter that can change this behavior.

So showing of the flash message defined above in a view is done by

// display success message
<?php if (Yii::$app->session->hasFlash('success')): ?>
    <div class="alert alert-success alert-dismissable">
         <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
         <h4><i class="icon fa fa-check"></i>Saved!</h4>
         <?= Yii::$app->session->getFlash('success') ?>
    </div>
<?php endif; ?>

// display error message
<?php if (Yii::$app->session->hasFlash('error')): ?>
    <div class="alert alert-danger alert-dismissable">
         <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
         <h4><i class="icon fa fa-check"></i>Saved!</h4>
         <?= Yii::$app->session->getFlash('error') ?>
    </div>
<?php endif; ?>

Использование flash-cообщений

Когда вы редактируете модель с помощью формы, удаляете модель или выполняете любую другую операцию, хорошо сообщить пользователям, прошла ли она хорошо или произошла ошибка. Как правило, после какого-то действия, такого как редактирование формы, произойдет перенаправление, и нам нужно отобразить сообщение на странице, на которую мы хотим перейти. Однако, как мы передаем его от текущей страницы к цели перенаправления и очищаем впоследствии? Флэш-сообщения помогут нам сделать это.

Подготовка

Создайте новое приложение с помощью composer, как описано в официальном руководстве по http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
по русски http://yiiframework.domain-na.me/doc/guide/2.0/ru/start-installation

Как это сделать…

1 Давайте создадим @app/controllers/TestController.php контроллер следующим образом:

<?php
namespace appcontrollers; use Yii;
use yiiwebController;
use yiifiltersAccessControl;
class TestController extends Controller {
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                    'allow' => true,
                    'roles ' => ['@'],
                    'actions' => ['user']
                    ],
                    [
                    'allow' => true,
                    'roles' => ['?'],
                    'actions' => ['index', 'success', 'error']
                    ],
                ],
                'denyCallback' => function ($rule, $action) {
                    Yii::$app->session->setFlash('error', 'This section is only for registered users.');
                    $this->redirect(['index']);
                },
            ],
        ];
    }
    public function actionUser()
    {
        return $this->renderContent('user')
    }

    public function actionSuccess()
    {
        Yii::$app->session->setFlash('success', 'Everything went fine!'); 
        $this->redirect(['index']);
    }

    public function actionError()
    {
        Yii::$app->session->setFlash('error', 'Everything went wrong!'); 
        $this->redirect(['index']);
    }
    public function actionIndex()
    {
        return $this->render('index');
    }
}

2 Кроме того, создайте @app/views/common/alert. php посмотреть следующим образом:

<?php
use yiibootstrapAlert;
?>
<?php if (Yii::$app->session->hasFlash('success')):?>
    <?= Alert::widget([
        'options' => ['class' => 'alert-success'],
        'body' => Yii::$app->session->getFlash('success'),]);
    ?>
<?php endif ?>
<?php if (Yii::$app->session->hasFlash('error')) :?>
    <?= Alert::widget([
        'options' => ['class' => 'alert-danger'],
        'body' => Yii::$app->session->getFlash('error'),
    ]);?>
<?php endif; ?>

3 Создайте @app/views/test/index.php файл представления следующим образом:

<?php
/* @var $this yiiwebView */
?>
<?= $this->render('//common/alert') ?>
<h2>Guest page</h2>
<p>There's a content of guest page</p>

4 Создайте @app/views/test/user.php файла представления следующим образом:

<?php
/* @var $this yiiwebView */
?>
<?= $this->render('//common/alert') ?>
<h2>User page</h2>
<p>There's a content of user page</p>

5 Теперь, если вы перейдете на http://yii-book.app/index.php?r=test/success, вы будете перенаправлены на http://yii-book.app/index.php?r=test/index и сообщение об успешном выполнении будет отображаться как

6 Более того, если вы заходите на http://yii-book.app/index. php?r=test/error, вы будете перенаправлены на ту же страницу, но с сообщением об ошибке. Обновление страницы индекса скроет сообщение

7 Затем попробуйте запустить http://yii-book.app/index.php?r=test/user. Вы будете перенаправлены на http://yii-book.app/index.php?r=test/index и сообщение об ошибке, выполненные в функции denyCallback:

Как это работает…

Мы установили флэш-сообщение с Yii::$app->session->(«sucess», » Все прошло хорошо!’). Внутренне, он сохраняет сообщение в состояние сеанса, поэтому на самом низком уровне наше сообщение хранится в $_SESSION до тех пор, пока Yii::$app->session->getFlash( ‘success’) не будет вызван и ключ $_SESSION не будет удален.
Флэш-сообщение будет автоматически удалено после получения запроса.

Есть еще.

Метод getAllFlashes()
Иногда нужно справиться со всеми вспышками. Вы можете сделать это простым способом, следующим образом:

$flashes = Yii::$app->session->getAllFlashes();
<?php foreach ($flashes as $key => $message): ?>
    <?= Alert::widget([
        'options' => ['class' => 'alert-info'],
        'body' => $message,
        ]);
    ?>
<?php endforeach; ?>

Метод removeAllFlashes()
Когда вам нужно очистить все ваши вспышки, используйте следующее:

Yii::$app->session->removeAllFlashes();

Метод removeFlash()
Если необходимо удалить метод flash с указанным ключом, используйте следующее:

Yii::$app->session->removeFlash('success');

В этом примере мы добавили очень полезную функцию обратного вызова, которая устанавливает сообщение об ошибке и выполняет перенаправление на страницу test/index.

Смотрите так же

Для получения дополнительной информации обратитесь к:

  • http://www.viiframework.com/doc-2.0/vii-web-session.html
  • http://www.yiiframework.com/doc-2.0/yii-bootstrap-alert.html

Using Flash messages in Yii 1.1.* is something that I have done for so long, and so commonly, that I take it completely for granted. Whenever a user performs some action and the processing generates some information I want to convey back, I plug that information into a success, warning, info or error flash message, pop in a redirect for that situation and VOILA! After my processes are finished, the flash message is displayed to the user.

For example, in Yii 1.1.*, I have a handy action that is called if a user needs to retrieve their password from the database. It was written well back, around 1.0.6-ish, but still gets the job done nicely. Look up the user by the posted email, if the user is found, send the user an email and set a success message, otherwise, set an error message. Once that’s done, redirect the user to the previous page. Otherwise, notify them that there was an error and keep going on.

When I converted this particular action to Yii2 it kept losing my flash messages. If I took out the redirect after successfully processing and sending the email, it was fine — but I need the redirect to prevent inadvertent multiple form submissions etc. What to do?!? After scouring the internet and Yii forums, and finding multiple instances of people complaining about it not working but no good solutions or notes other than ‘working as intended’, I finally — FINALLY — realized that it is the redirect method itself that is, while stopping any output to the screen, not actually stopping the currently processed action. If I was more observant, I would have noticed it in the example of a controller redirect here: http://www.yiiframework.com/doc-2.0/guide-structure-controllers.html

If you are starting with Yii at version 2.* this may not trip you up at all. But if you’re used to the old way, it’s quite a sneaky gotcha!

So, the moral of the story is: $controller->redirect() DOES NOT exit the current action the way it did in Yii 1.1.*, in Yii 2.* you must return $this->redirect() instead!

The Yii 1.0.6+ (circa 2011) version (truncated for brevity :p):

/**
 * actionResetPassword
 * Send user a link to change their forgotten password
 */
 public function actionResetPassword()
 {
   $unknownUser = new User();

    if( isset($_POST['User']) )
    {
       /* ... do some processing here ... */                  
       if ( $found ){
          if( $registeredUser->sendStudentPassReminderEmail() )
             Yii::app()->user->setFlash('success', 'blah blah good');
          else
             Yii::app()->user->setFlash('error', 'blah blah bad');
          $this->redirect( Yii::app()->user->getReturnUrl() );
       }
       else
       {
          Yii::app()->user->setFlash('error', "Some Error Message");
       }
    }
  }

  Yii::app()->user->setReturnUrl(  Yii::app()->getRequest()->getUrlReferrer() );
  $this->render('sendPasswordResetCode', array('model'=>$unknownUser));
}

The Yii2 version becomes:

public function run()
{
  $unknownUser = new User();

  if ( $unknownUser->load(Yii::$app->request->post()) && $unknownUser->email )
  {
    if ( $registeredUser = User::find()->where(['email'=>$unknownUser->email])->one() )
    {
      if ( $this->sendStudentPassReminderEmail( $registeredUser ) )
      {
        Yii::$app->session->addFlash( 'success', 'blah blah good' );
        // THIS IS THE MAGIC LINE -- NOTE THE RETURN
        return $this->controller->redirect( Url::previous('before-forgot-password'));
      } else
      {
        Yii::$app->session->addFlash( 'error', 'blah blah bad' );
      }
    } else {
      Yii::$app->session->addFlash('error','Some Error Message');
    } 
  }

  Url::remember( Yii::$app->request->referrer ,'before-forgot-password' );
  return $this->controller->render( 'forgot-password' ,['model'=>$unknownUser]);
}

Sometimes, it’s the little things in life that cause the biggest stumbles :p

У меня есть приложение, использующее Yii2, основной шаблон. В моем приложении я использую Yii::$app->session-setFlash для показа сообщения, когда оказывать страница.

Когда я размещаю свое заявление в app, это работает хорошо. Но когда я перемещаю приложение в module, это не показывало сообщение. Модуль называется school

Это код, который я использую для отображения сообщения и отображения страницы в моем app/module/school

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

страница успешно вернулась к school/student/create страницу, но не показывал сообщение.

и этот код, когда я размещаю приложение в app

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

код выше успешно вернуться на страницу student/create и показать сообщение.

Это моя структура каталогов приложения:

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

Кто-нибудь знает, почему это случилось? и как я могу решить это?

Любая помощь будет оценена, спасибо 🙂

Отредактировано:

Это код:

приложение / модули / школа / вид / студент / _form.php

<?php

use yiihelpersHtml;
use yiiwidgetsActiveForm;
use kartikfileFileInput;

/* @var $this yiiwebView */
/* @var $model backendmodelsStudent */
/* @var $form yiiwidgetsActiveForm */
?>

<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>

а также

приложение / модули / школа / просмотр / студент / create.php

<?php
use yiihelpersHtml;

/* @var $this yiiwebView */
/* @var $model backendmodelsStudent */

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

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

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

0

Решение

У вас нет строки кода для рендеринга ваших сессионных флеш-сообщений в модуле. Чтобы проверить это, добавьте следующий код в приложение / modules / school / view / student / create.php:

    <?php
use yiihelpersHtml;

/* @var $this yiiwebView */
/* @var $model backendmodelsStudent */

$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,
])
?>

Я рекомендую добавить следующий код в виджет и отобразить виджет в шаблоне макета (app / views / layouts / main.php) или добавить в шаблон макета без виджета:

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

0

Другие решения

Других решений пока нет …

Flash-сообщения в Yii (flash-messages) позволяют передавать некоторый текстовый контент из контроллера в отображение. Речь, конечно же, не идет о передаче статей и больших кусков текста. Здесь подразумеваются короткие системные сообщения, например, об успешном сохранении данных или отправке формы. После того, как сообщение будет выведено на странице оно удаляется.

Мы можем передавать несколько различных сообщений, которые будут отображаться в различных местах на странице, но для этого каждому message мы должны присвоить свой уникальный идентификатор – «ключ».

Например, передать сообщение о том, что логин был неверным, мы можем следующим образом:

1
  Yii::app()->user->setFlash('error', "Логин не существует!");

Данная строчка должна идти в действии контроллера до вызова отображения.

В самом отображении мы проверяем, существует ли сообщение с ключом «error» и, если существует, выводим его. Для этого мы используем методы hasFlash() и getFlash():

1
2
3
4
5
<?php if(Yii::app()->user->hasFlash('error')):?>
    <span class="error_message">
        <?php echo Yii::app()->user->getFlash('error'); ?>
    </span>
<?php endif; ?>

Если нам нужно передать вместе с сообщением об ошибке также еще какие-то другие сообщения, то передаем их, используя свои «ключи»:

1
2
3
Yii::app()->user->setFlash('error', "Логин не существует!");
Yii::app()->user->setFlash('my_name', "Иван");
Yii::app()->user->setFlash('message3', "Это третье сообщение");

В отображении мы можем также сохранить все переданные сообщения в массив и затем вывести их из него. Для сохранения сообщений в массив используем getFlashes():

1
2
3
4
5
6
$allMessages = Yii::app()->user->getFlashes();
if ($allMessages) {
    foreach($allMessages as $key => $message) {
        echo '<span>' . $key.': '.$message . '</span><br>';
    }
}

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Yii2 model save error
  • Yii2 model add error
  • Yii2 fullcalendar error dist
  • Yii2 field error message
  • Yii2 exception error

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии