@@ -25,7 +25,7 @@ First, enable form login under your firewall:
2525 # app/config/security.yml
2626 security :
2727 # ...
28-
28+
2929 firewalls :
3030 default :
3131 anonymous : ~
@@ -98,7 +98,7 @@ under your ``form_login`` configuration (``/login`` and ``/login_check``):
9898.. configuration-block ::
9999
100100 .. code-block :: php-annotations
101-
101+
102102 // src/AppBundle/Controller/SecurityController.php
103103 // ...
104104 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
@@ -165,28 +165,16 @@ form::
165165
166166 // src/AppBundle/Controller/SecurityController.php
167167 // ...
168-
169- // ADD THIS use STATEMENT above your class
170- use Symfony\Component\Security\Core\Security;
171168
172169 public function loginAction(Request $request)
173170 {
174- $session = $request->getSession( );
171+ $authenticationUtils = $this->get('security.authentication_utils' );
175172
176173 // get the login error if there is one
177- if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
178- $error = $request->attributes->get(
179- Security::AUTHENTICATION_ERROR
180- );
181- } elseif (null !== $session && $session->has(Security::AUTHENTICATION_ERROR)) {
182- $error = $session->get(Security::AUTHENTICATION_ERROR);
183- $session->remove(Security::AUTHENTICATION_ERROR);
184- } else {
185- $error = '';
186- }
174+ $error = $authenticationUtils->getLastAuthenticationError();
187175
188176 // last username entered by the user
189- $lastUsername = (null === $session) ? '' : $session->get(Security::LAST_USERNAME );
177+ $lastUsername = $authenticationUtils->getLastUsername( );
190178
191179 return $this->render(
192180 'security/login.html.twig',
@@ -198,6 +186,11 @@ form::
198186 );
199187 }
200188
189+ .. versionadded :: 2.6
190+ The ``security.authentication_utils `` service and the
191+ :class: `Symfony\\ Component\\ Security\\ Http\\ Authentication\\ AuthenticationUtils `
192+ class were introduced in Symfony 2.6.
193+
201194Don't let this controller confuse you. As you'll see in a moment, when the
202195user submits the form, the security system automatically handles the form
203196submission for you. If the user had submitted an invalid username or password,
@@ -471,4 +464,4 @@ any firewall. This means you can't check for security or even access the
471464user object on these pages. See :doc: `/cookbook/controller/error_pages `
472465for more details.
473466
474- .. _`FOSUserBundle` : https://github.com/FriendsOfSymfony/FOSUserBundle
467+ .. _`FOSUserBundle` : https://github.com/FriendsOfSymfony/FOSUserBundle
0 commit comments