Skip to content
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

Merged
merged 10 commits into from
Sep 7, 2015
12 changes: 12 additions & 0 deletions app/Resources/translations/messages.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@
<source>post.no_posts_found</source>
<target>No posts found.</target>
</trans-unit>
<trans-unit id="post.created_successfully">
<source>post.created_successfully</source>
<target>Post created successfully!</target>
</trans-unit>
<trans-unit id="post.updated_successfully">
<source>post.updated_successfully</source>
<target>Post updated successfully!</target>
</trans-unit>
<trans-unit id="post.deleted_successfully">
<source>post.deleted_successfully</source>
<target>Post deleted successfully!</target>
</trans-unit>

<trans-unit id="help.app_description">
<source>help.app_description</source>
Expand Down
11 changes: 11 additions & 0 deletions app/Resources/translations/messages.ru.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@
<trans-unit id="post.no_posts_found">
<source>post.no_posts_found</source>
<target>Ни одной записи не найдено.</target>
<trans-unit id="post.created_successfully">
<source>post.created_successfully</source>
<target>Запись успешно создана!</target>
</trans-unit>
<trans-unit id="post.updated_successfully">
<source>post.updated_successfully</source>
<target>Запись успешно обновлена!</target>
</trans-unit>
<trans-unit id="post.deleted_successfully">
<source>post.deleted_successfully</source>
<target>Запись успешно удалена!</target>
</trans-unit>

<trans-unit id="help.app_description">
Expand Down
2 changes: 2 additions & 0 deletions app/Resources/views/admin/blog/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{% block main %}
<h1>{{ 'title.edit_post'|trans({'%id%': post.id}) }}</h1>

{{ helper.render_flash_messages() }}

{{ include('admin/blog/_form.html.twig', {
form: edit_form,
button_label: 'action.save'|trans,
Expand Down
2 changes: 2 additions & 0 deletions app/Resources/views/admin/blog/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{% block main %}
<h1>{{ 'title.post_list'|trans }}</h1>

{{ helper.render_flash_messages() }}
Copy link
Member

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

Copy link
Member Author

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!


<table class="table table-striped">
<thead>
<tr>
Expand Down
6 changes: 6 additions & 0 deletions app/Resources/views/admin/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Copy link
Member

Choose a reason for hiding this comment

The 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') }}">
Expand Down
25 changes: 25 additions & 0 deletions app/Resources/views/macro/messages.html.twig
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') %}
Copy link
Member

Choose a reason for hiding this comment

The 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 if app.session.started check

Copy link
Member Author

Choose a reason for hiding this comment

The 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 if ... trick.

Copy link
Member

Choose a reason for hiding this comment

The 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">&times;</span>
</button>
{{ text }}
</div>
{% endmacro %}
9 changes: 9 additions & 0 deletions src/AppBundle/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Member Author

Choose a reason for hiding this comment

The 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:

You can also store small messages that will be stored on the user's session for exactly one additional request. [...]

[...] By design, flash messages are meant to live for exactly one request (they're "gone in a flash").

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it's not merged yet (see symfony/symfony-docs#5640).

Copy link
Member Author

Choose a reason for hiding this comment

The 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');
}

Expand Down Expand Up @@ -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()));
}

Expand Down Expand Up @@ -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');
Expand Down