-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add flash messages #163
Add flash messages #163
Changes from 6 commits
0680841
12bd670
a46c948
7507a50
51f7262
da04216
f481eef
d01311b
9a8d43d
3eefa76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,12 @@ | |
#} | ||
{% extends 'base.html.twig' %} | ||
|
||
{# | ||
Import macros used for displaying flash messages. | ||
See http://twig.sensiolabs.org/doc/tags/import.html | ||
#} | ||
{% import 'macro/messages.html.twig' as helper %} | ||
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. same comment that in the original PR. Importing macros in the parent to use them in the child is not a supported way to use macros and will be removed in Twig 2.x |
||
|
||
{% block header_navigation_links %} | ||
<li> | ||
<a href="{{ path('admin_post_index') }}"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{# | ||
Macros are comparable with functions in regular programming languages. | ||
They are useful to put often used HTML idioms into reusable elements | ||
to not repeat yourself. | ||
See http://twig.sensiolabs.org/doc/tags/macro.html | ||
#} | ||
{% macro render_flash_messages() %} | ||
{% from _self import alert %} | ||
|
||
<div class="messages"> | ||
{% for flash_message in app.session.flashBag.get('success') %} | ||
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. this will start a session even if the user has no session yet. You should wrap this in a 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. Good catch. I still belive this is a Symfony bug ... but until it's fixed, let's use the 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. well, you are asking to read from the session. currently, Symfony requires to start the session to read from it. There is proposal to change it, but it cannot be considered as a bugfix (it is a big refactoring), and I'm not sure it is ready. |
||
{{ alert(flash_message|trans) }} | ||
{% endfor %} | ||
</div> | ||
{% endmacro %} | ||
|
||
{% macro alert(text, class = 'success') %} | ||
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #} | ||
<div class="alert alert-dismissible alert-{{ class }}" role="alert"> | ||
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
<span aria-hidden="true">×</span> | ||
</button> | ||
{{ text }} | ||
</div> | ||
{% endmacro %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,11 @@ public function newAction(Request $request) | |
$em->persist($post); | ||
$em->flush(); | ||
|
||
// Flash messages are used to notify the user about the result of the | ||
// actions. They only last as long as the next request. | ||
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. We should improve this description like we did in the docs (flash messages last until they are retrieved from the bag). 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. Where can I find the updated explanation? In http://symfony.com/doc/current/book/controller.html#flash-messages I read:
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. Sorry, it's not merged yet (see symfony/symfony-docs#5640). 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. Thanks for the reference. I've just reworded this help note in 3eefa76. |
||
// See http://symfony.com/doc/current/book/controller.html#flash-messages | ||
$this->addFlash('success', 'post.created_successfully'); | ||
|
||
return $this->redirectToRoute('admin_post_index'); | ||
} | ||
|
||
|
@@ -142,6 +147,8 @@ public function editAction(Post $post, Request $request) | |
$post->setSlug($this->get('slugger')->slugify($post->getTitle())); | ||
$em->flush(); | ||
|
||
$this->addFlash('success', 'post.updated_successfully'); | ||
|
||
return $this->redirectToRoute('admin_post_edit', array('id' => $post->getId())); | ||
} | ||
|
||
|
@@ -173,6 +180,8 @@ public function deleteAction(Request $request, Post $post) | |
|
||
$em->remove($post); | ||
$em->flush(); | ||
|
||
$this->addFlash('success', 'post.deleted_successfully'); | ||
} | ||
|
||
return $this->redirectToRoute('admin_post_index'); | ||
|
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.
Rather than using a macro, I suggest using an include of a template rendering flash messages
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.
I had some doubts about this. But after reading your comment, I've created the template and removed te macros. We'll add some macros in other feature. Thanks!