I'm working on a Symfony2.3.6 project, and it works good. I've made a backend side with a few CRUD for some Entities, and it also work good.
Now what I want to do is notify an user when an operation is made on an entity. So I want to notify when an entity is saved, updated or deleted, as Symfony1.4 made. I was in doubt where to put the flashbag message, if in the entity or in the controller or even with events?!
Which is the right place where I can put this kind of feature, and how I can do it? Thanks...
The documentation describes perfectly how to store and display these messages in your controller.
public function updateAction(Request $request)
{
$form = $this->createForm(...);
$form->handleRequest($request);
if ($form->isValid()) {
// do some sort of processing
$this->get('session')->getFlashBag()->add(
'notice',
'Your changes were saved!'
);
return $this->redirect($this->generateUrl(...));
}
return $this->render(...);
}
% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash-notice">
{{ flashMessage }}
</div>
{% endfor %}
You can use different flashbags for other messages, for example an error :
[...]
$this->get('session')->getFlashBag()->add(
'delete',
'The entity has been deleted!'
);
[...]
% for flashMessage in app.session.flashbag.get('delete') %}
<div class="flash-notice delete">
{{ flashMessage }}
</div>
{% endfor %}
Use CSS to style the delete class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With