Flash Messages and Session
Flash Messages and Session
~11 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026
To wrap up block 2, let's solve a very practical problem: showing a success message AFTER a redirect – the classic post/redirect/get pattern.
The problem without flash messages
After creating a project (chapter 11), we redirect via redirectToRoute('project_index') – but how do we show "Project created successfully!" on the NEW page? The message needs to "survive" the redirect.
Flash messages: the solution
#[Route('/projects/new', name: 'project_new', methods: ['POST'])]
public function new(Request $request): Response
{
// ... create the project ...
$this->addFlash('success', 'Project created successfully!');
return $this->redirectToRoute('project_index');
}addFlash('success', ...) stores the message in the SESSION under the category 'success' (freely chosen, common ones are success, error, warning, info). A flash message gets displayed EXACTLY ONCE – on the NEXT read, it's automatically removed from the session.
Displaying flash messages in Twig (a preview of chapter 13)
<body>
{% for label, messages in app.flashes %}
{% for message in messages %}
<div class="alert alert-{{ label }}">{{ message }}</div>
{% endfor %}
{% endfor %}
{% block body %}{% endblock %}
</body>app.flashes is a Twig variable automatically available in EVERY template – since we place it in the base.html.twig layout (chapter 14 explains Twig inheritance), EVERY flash message automatically shows up on EVERY page, without every single controller having to worry about it.
Using the session directly
Beyond flash messages, the session can also be used for LONGER-lived data – e.g. a recently selected project that should be remembered across several requests:
use Symfony\Component\HttpFoundation\Session\SessionInterface;
#[Route('/projects/{id}', name: 'project_show', requirements: ['id' => '\d+'])]
public function show(int $id, SessionInterface $session): Response
{
$session->set('last_project_id', $id);
return new Response('Project details for ID ' . $id);
}// Elsewhere, e.g. on the dashboard:
$lastId = $session->get('last_project_id');
if ($lastId !== null) {
// show a "continue where you left off" link
}Achtung: Session data lives SERVER-SIDE (by default as files under var/cache/sessions/) and is tied to ONE browser via the session cookie. For data that should be shared between DIFFERENT devices/browsers of a user, the database (block 4) is the right place, NOT the session.
Flash message vs. session: a rule of thumb
| Tool | Lifetime |
|---|---|
| Flash message | One-off message for the NEXT request, gets auto-deleted after – ideal for success/error messages after a redirect. |
| Session (direct) | Longer-lived data across MULTIPLE requests, persists until explicitly cleared or the session expires. |
With that, block 2 (routing & controllers) is complete! Block 3 covers Twig and forms – finally our pages will look like REAL web pages, instead of just returning plain text responses.