Commit 1cfa5bc9 by Qiang Xue

Fixes #4740: Added `yii\web\Session::addFlash()`

parent f1aeac5d
...@@ -183,9 +183,10 @@ Yii Framework 2 Change Log ...@@ -183,9 +183,10 @@ Yii Framework 2 Change Log
- Enh #4607: AR model will throw an exception if it does not have a primary key to avoid updating/deleting data massively (qiangxue) - Enh #4607: AR model will throw an exception if it does not have a primary key to avoid updating/deleting data massively (qiangxue)
- Enh #4630: Added automatic generating of unique slug value to `yii\behaviors\Sluggable` (klimov-paul) - Enh #4630: Added automatic generating of unique slug value to `yii\behaviors\Sluggable` (klimov-paul)
- Enh #4636: Added `yii\web\Response::setDownloadHeaders()` (pawzar) - Enh #4636: Added `yii\web\Response::setDownloadHeaders()` (pawzar)
- Enh #4644: Added `\yii\db\Schema::createColumnSchema()` to be able to customize column schema used (mcd-php) - Enh #4644: Added `yii\db\Schema::createColumnSchema()` to be able to customize column schema used (mcd-php)
- Enh #4656: HtmlPurifier helper config can now be a closure to change the purifier config object after it was created (Alex-Code) - Enh #4656: HtmlPurifier helper config can now be a closure to change the purifier config object after it was created (Alex-Code)
- Enh #4691: Encoding on `ActiveForm` and `ActiveField` validation errors is now configurable (Alex-Code) - Enh #4691: Encoding on `ActiveForm` and `ActiveField` validation errors is now configurable (Alex-Code)
- Enh #4740: Added `yii\web\Session::addFlash()` (restyler)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
......
...@@ -699,9 +699,10 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -699,9 +699,10 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
} }
/** /**
* Stores a flash message. * Sets a flash message.
* A flash message will be automatically deleted after it is accessed in a request and the deletion will happen * A flash message will be automatically deleted after it is accessed in a request and the deletion will happen
* in the next request. * in the next request.
* If there is already an existing flash message with the same key, it will be overwritten by the new one.
* @param string $key the key identifying the flash message. Note that flash messages * @param string $key the key identifying the flash message. Note that flash messages
* and normal session variables share the same name space. If you have a normal * and normal session variables share the same name space. If you have a normal
* session variable using the same name, its value will be overwritten by this method. * session variable using the same name, its value will be overwritten by this method.
...@@ -722,14 +723,14 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -722,14 +723,14 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
} }
/** /**
* Appends new flash message to the specified key. * Adds a flash message.
* If there are existing flash messages with the same key, the new one will be appended to the existing message array.
* @param string $key the key identifying the flash message. * @param string $key the key identifying the flash message.
* @param mixed $value flash message * @param mixed $value flash message
* @param boolean $removeAfterAccess whether the flash message should be automatically removed only if * @param boolean $removeAfterAccess whether the flash message should be automatically removed only if
* it is accessed. If false, the flash message will be automatically removed after the next request, * it is accessed. If false, the flash message will be automatically removed after the next request,
* regardless if it is accessed or not. If true (default value), the flash message will remain until after * regardless if it is accessed or not. If true (default value), the flash message will remain until after
* it is accessed. * it is accessed.
* @throws InvalidParamException if exising session variable is not array.
* @see getFlash() * @see getFlash()
* @see removeFlash() * @see removeFlash()
*/ */
...@@ -737,16 +738,16 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -737,16 +738,16 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
{ {
$counters = $this->get($this->flashParam, []); $counters = $this->get($this->flashParam, []);
$counters[$key] = $removeAfterAccess ? -1 : 0; $counters[$key] = $removeAfterAccess ? -1 : 0;
if (!empty($_SESSION[$key])) { $_SESSION[$this->flashParam] = $counters;
// If it's not an array, convert it to array if (empty($_SESSION[$key])) {
if (!is_array($_SESSION[$key])) {
$_SESSION[$key] = [$_SESSION[$key]];
}
$_SESSION[$key][] = $value;
} else {
$_SESSION[$key] = [$value]; $_SESSION[$key] = [$value];
} else {
if (is_array($_SESSION[$key])) {
$_SESSION[$key][] = $value;
} else {
$_SESSION[$key] = [$_SESSION[$key], $value];
}
} }
$_SESSION[$this->flashParam] = $counters;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment