diff --git a/security/access_denied_handler.rst b/security/access_denied_handler.rst index c880ec14065..de875fb01f7 100644 --- a/security/access_denied_handler.rst +++ b/security/access_denied_handler.rst @@ -46,7 +46,10 @@ unauthenticated user tries to access a protected resource:: public function start(Request $request, AuthenticationException $authException = null): RedirectResponse { // add a custom flash message and redirect to the login page - $request->getSession()->getFlashBag()->add('note', 'You have to login in order to access this page.'); + $session = $request->getSession(); + if ($session instanceof FlashBagAwareSessionInterface) { + $session->getFlashBag()->add('note', 'You have to login in order to access this page.'); + } return new RedirectResponse($this->urlGenerator->generate('security_login')); } diff --git a/session.rst b/session.rst index da6e3e201c7..3e65ce552a4 100644 --- a/session.rst +++ b/session.rst @@ -163,6 +163,11 @@ if you type-hint an argument with :class:`Symfony\\Component\\HttpFoundation\\Re // the second argument is the value returned when the attribute doesn't exist $filters = $session->get('filters', []); + // when the session is flashBag aware, you can add a flash message + if ($session instanceof FlashBagAwareSessionInterface) { + $session->getFlashBag()->add('note', 'This is a flash message.'); + } + // ... } }