Add template to symfony2 flash messages

After creating and deleting the object, I redirect to the same action (indexAction). After creating, I would like to make this boot message in the form of flash messages:

<div class="alert alert-success" role="alert"> <a href="#" class="alert-link">...</a> </div> 

but after deleting I would like to make another html block:

 <div class="alert alert-danger" role="alert"> <a href="#" class="alert-link">...</a> </div> 

What is the best way to send this to flash messages? Because, I think that passing all html is not a good idea?

 $this->get('session')->getFlashBag()->add( 'notice', '<div class="alert alert-danger" role="alert"> <a href="#" class="alert-link">...</a> </div>' ); 

Is there any other way to solve this problem?

+6
source share
3 answers

I have found the best solution.

In my controller:

 $this->get('session')->getFlashBag()->add( 'notice', array( 'alert' => 'success', 'title' => 'Success!', 'message' => 'New word has been added successfully.' ) ); 

This is my opinion:

 {% if app.session.started %} {% for flashMessage in app.session.flashbag.get('notice') %} <div class="alert alert-{{ flashMessage.alert }} alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"> <span aria-hidden="true">&times;</span><span class="sr-only">Close</span> </button> <strong>{{ flashMessage.title }}</strong> {{ flashMessage.message }} </div> {% endfor %} {% endif %} 
+4
source

Here is what I usually do:

 {% for type, flashes in app.session.flashbag.all %} {% for flash in flashes %} <div class="alert alert-{{ type }} fade in"> {{ flash }} </div> {% endfor %} {% endfor %} 

From the controller:

 $this->addFlash('success', 'What an awesome message !'); 

Generates alert-success

+9
source

In your controller

 $this->get('session')->getFlashBag()->add('info', 'info message'); 

In your opinion

 {% for message in app.session.flashbag.get('info') %} <div class="alert alert-info alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert"> <span aria-hidden="true">&times;</span> <span class="sr-only">Close</span> </button> <p>{{ message }}</p> </div> {% endfor %} 
+6
source

Source: https://habr.com/ru/post/973572/


All Articles