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>

{{ include('default/_flash_messages.html.twig') }}

{{ 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>

{{ include('default/_flash_messages.html.twig') }}

<table class="table table-striped">
<thead>
<tr>
Expand Down
14 changes: 14 additions & 0 deletions app/Resources/views/default/_flash_messages.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% if app.session.started %}
<div class="messages">
{% for message in app.session.flashBag.get('success') %}
{# Bootstrap alert, see http://getbootstrap.com/components/#alerts #}
<div class="alert alert-dismissible alert-success" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>

{{ message|trans }}
</div>
{% endfor %}
</div>
{% endif %}
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