I set a flash message in a controller with the following code:
$this->get('session')->getFlashBag()->add('success', 'Message sent successfully');
And in my template, I use the following to (attempt to) display it:
{% if app.session.flashbag.has('success') %}
<div id="flash">
{{ app.session.flashbag.get('success') }}
</div>
{% endif %}
The problem is that, despite the API documentation stating that get
returns a string, I'm getting an array to string conversion exception. If I change the code in the template to:
{% for flashMessage in app.session.flashbag.get('success') %}
<div id="flash">
{{ flashMessage }}
</div>
{% endfor %}
It works perfectly. I'd rather not use a loop here since I'm only ever either going to have the single message or not.
Is there a solution where I can just check for the existence of a single flash message and display it if it's there? Or am I stuck with a useless loop?