After the create and delete object, I redirect to the same action(indexAction). After create I would like render this bootstrap messages as flash messages:
<div class="alert alert-success" role="alert">
<a href="#" class="alert-link">...</a>
</div>
but after delete, I would like render another block of html:
<div class="alert alert-danger" role="alert">
<a href="#" class="alert-link">...</a>
</div>
What is the best way to pass this to flash messages? Because, I think passing all html is not good idea?
$this->get('session')->getFlashBag()->add(
'notice',
'<div class="alert alert-danger" role="alert">
<a href="#" class="alert-link">...</a>
</div>'
);
Is any exist better way to solve this problem?
Here is what I usualy 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 a controller:
$this->addFlash('success', 'What an awesome message !');
Will create an alert-success
In your controller
$this->get('session')->getFlashBag()->add('info', 'info message');
In your view
{% 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">×</span>
<span class="sr-only">Close</span>
</button>
<p>{{ message }}</p>
</div>
{% endfor %}
I have found better 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 view:
{% 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">×</span><span class="sr-only">Close</span>
</button>
<strong>{{ flashMessage.title }}</strong> {{ flashMessage.message }}
</div>
{% endfor %}
{% endif %}
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