Commit ad6b1910 by Carsten Brandt

improved docs about flash messages

parent 5303506c
...@@ -591,7 +591,6 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -591,7 +591,6 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
public function has($key) public function has($key)
{ {
$this->open(); $this->open();
return isset($_SESSION[$key]); return isset($_SESSION[$key]);
} }
...@@ -625,6 +624,10 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -625,6 +624,10 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
* @param boolean $delete whether to delete this flash message right after this method is called. * @param boolean $delete whether to delete this flash message right after this method is called.
* If false, the flash message will be automatically deleted after the next request. * If false, the flash message will be automatically deleted after the next request.
* @return mixed the flash message * @return mixed the flash message
* @see setFlash()
* @see hasFlash()
* @see getAllFlashes()
* @see removeFlash()
*/ */
public function getFlash($key, $defaultValue = null, $delete = false) public function getFlash($key, $defaultValue = null, $delete = false)
{ {
...@@ -643,7 +646,26 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -643,7 +646,26 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
/** /**
* Returns all flash messages. * Returns all flash messages.
*
* You may use this method to display all the flash messages in a view file:
*
* ```php
* <?php
* foreach(Yii::$app->session->getAllFlashes() as $key => $message) {
* echo '<div class="alert alert-' . $key . '">' . $message . '</div>';
* } ?>
* ```
*
* With the above code you can use the [bootstrap alert][] classes such as `success`, `info`, `danger`
* as the flash message key to influence the color of the div.
*
* [bootstrap alert]: http://getbootstrap.com/components/#alerts
*
* @return array flash messages (key => message). * @return array flash messages (key => message).
* @see setFlash()
* @see getFlash()
* @see hasFlash()
* @see removeFlash()
*/ */
public function getAllFlashes() public function getAllFlashes()
{ {
...@@ -665,6 +687,8 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -665,6 +687,8 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
* 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.
* @param mixed $value flash message * @param mixed $value flash message
* @see getFlash()
* @see removeFlash()
*/ */
public function setFlash($key, $value = true) public function setFlash($key, $value = true)
{ {
...@@ -681,6 +705,9 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -681,6 +705,9 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
* 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, it will be removed by this method. * session variable using the same name, it will be removed by this method.
* @return mixed the removed flash message. Null if the flash message does not exist. * @return mixed the removed flash message. Null if the flash message does not exist.
* @see getFlash()
* @see setFlash()
* @see removeAllFlashes()
*/ */
public function removeFlash($key) public function removeFlash($key)
{ {
...@@ -697,6 +724,9 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -697,6 +724,9 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
* Note that flash messages and normal session variables share the same name space. * Note that flash messages and normal session variables share the same name space.
* If you have a normal session variable using the same name, it will be removed * If you have a normal session variable using the same name, it will be removed
* by this method. * by this method.
* @see getFlash()
* @see setFlash()
* @see removeFlash()
*/ */
public function removeAllFlashes() public function removeAllFlashes()
{ {
......
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