Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Twig Bridge] A simpler way to retrieve flash messages
- Loading branch information
1 parent
69374da
commit 5a56b23
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,4 +145,35 @@ public function getDebug() | |
|
||
return $this->debug; | ||
} | ||
|
||
/** | ||
* Returns some or all the existing flash messages: | ||
* * getFlashes() returns all the flash messages | ||
* * getFlashes('notice') returns a simple array with flash messages of that type | ||
* * getFlashes(array('notice', 'error')) returns a nested array of type => messages. | ||
* | ||
* @return array | ||
*/ | ||
public function getFlashes($types = null) | ||
{ | ||
// needed to avoid starting the session automatically when looking for flash messages | ||
try { | ||
$session = $this->getSession(); | ||
if (null === $session || !$session->isStarted()) { | ||
return array(); | ||
} | ||
} catch (\RuntimeException $e) { | ||
return array(); | ||
} | ||
|
||
if (null === $types || '' === $types || array() === $types) { | ||
return $session->getFlashBag()->all(); | ||
} | ||
|
||
if (is_string($types)) { | ||
return $session->getFlashBag()->get($types); | ||
} | ||
|
||
return array_intersect_key($session->getFlashBag()->all(), array_flip($types)); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
javiereguiluz
Author
Member
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@javiereguiluz @fabpot Calling
FlashBag::all()
method will emptyFlashBag::$flashes
. Is it on purpose ?