Home | About Me
| | | | | | | | | |

Symfony 3: Flash messages

Hands-On | 14 Mar 2019
Tags: twig, symfony, ready, templates, flash-messages, error, notice


1) Setting the message

flashBag = $flashBag;
    }

    /**
     * @Route('/index', name="flash_index")
     */
    public function index()
    {
        return $this->render('flash/index.html.twig');
    }

    /**
     * @Route('/setFlash')
     */
    public function setFlashMessage()
    {
        $this->flashBag->add('notice', 'Here is the flash message.');
        $this->redirectToRoute('flash_index');
    }
}

2) Reading the message in the template

{# ./templates/flash/index.html.twig #}

{% for message in app.flashes('notice') %}
    
{{ message }}
{% endfor %}