Browse Source

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

tags/2.0.0-rc
Qiang Xue 10 years ago
parent
commit
1cfa5bc9ee
  1. 3
      framework/CHANGELOG.md
  2. 23
      framework/web/Session.php

3
framework/CHANGELOG.md

@ -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 #4630: Added automatic generating of unique slug value to `yii\behaviors\Sluggable` (klimov-paul)
- 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 #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: 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)

23
framework/web/Session.php

@ -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
* 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
* 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.
@ -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 mixed $value flash message
* @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,
* regardless if it is accessed or not. If true (default value), the flash message will remain until after
* it is accessed.
* @throws InvalidParamException if exising session variable is not array.
* @see getFlash()
* @see removeFlash()
*/
@ -737,16 +738,16 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
{
$counters = $this->get($this->flashParam, []);
$counters[$key] = $removeAfterAccess ? -1 : 0;
if (!empty($_SESSION[$key])) {
// If it's not an array, convert it to array
if (!is_array($_SESSION[$key])) {
$_SESSION[$key] = [$_SESSION[$key]];
}
$_SESSION[$key][] = $value;
} else {
$_SESSION[$this->flashParam] = $counters;
if (empty($_SESSION[$key])) {
$_SESSION[$key] = [$value];
} else {
if (is_array($_SESSION[$key])) {
$_SESSION[$key][] = $value;
} else {
$_SESSION[$key] = [$_SESSION[$key], $value];
}
}
$_SESSION[$this->flashParam] = $counters;
}
/**

Loading…
Cancel
Save