-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Twig Bridge] A simpler way to retrieve flash messages #21819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8ab885d
9d471dc
e05144d
e42bd57
27709f3
3a0b379
7acb794
16a794d
c916aee
20eba3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,4 +145,37 @@ public function getDebug() | |
|
||
return $this->debug; | ||
} | ||
|
||
/** | ||
* Returns some or all the existing flash messages. The method is variadic: | ||
* if no arguments are passed, all flashes are returned; if one or more | ||
* arguments are passed, only the flashes that belong to that category are | ||
* returned; e.g. getFlashes('notice') or getFlashes('notice', 'error'). | ||
* | ||
* @return array | ||
*/ | ||
public function getFlashes() | ||
{ | ||
try { | ||
$session = $this->getSession(); | ||
if (null !== $session && !$session->isStarted()) { | ||
return array(); | ||
} | ||
} catch(\RuntimeException $e) { | ||
return array(); | ||
} | ||
|
||
if (0 === func_num_args()) { | ||
return $session->getFlashBag()->all(); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should cast $types to an array first: $type = (array) $type; |
||
$flashes = array(); | ||
foreach ($session->getFlashBag()->all() as $key => $message) { | ||
if (in_array($key, func_get_args())) { | ||
$flashes[$key][] = $message; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling this method with a string like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, @HeahDude commented meanwhile :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way.. offset 0 may not exist :) What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh and it violates the api (more or less) with edit: yeah.. already pointed out by @stof , but it's now advertised with
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ro0NL good catch about the potential non-existent 0 offset. I've added |
||
|
||
return $flashes; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also we could support array of categories? i.e:
getFlashes(['notice', 'error'])
, sometimes this information is configurable.