@@ -25,7 +25,7 @@ First, enable form login under your firewall:
25
25
# app/config/security.yml
26
26
security :
27
27
# ...
28
-
28
+
29
29
firewalls :
30
30
default :
31
31
anonymous : ~
@@ -98,7 +98,7 @@ under your ``form_login`` configuration (``/login`` and ``/login_check``):
98
98
.. configuration-block ::
99
99
100
100
.. code-block :: php-annotations
101
-
101
+
102
102
// src/AppBundle/Controller/SecurityController.php
103
103
// ...
104
104
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
@@ -165,39 +165,35 @@ form::
165
165
166
166
// src/AppBundle/Controller/SecurityController.php
167
167
// ...
168
-
168
+
169
169
// ADD THIS use STATEMENT above your class
170
170
use Symfony\Component\Security\Core\Security;
171
171
172
172
public function loginAction(Request $request)
173
173
{
174
- $session = $request->getSession( );
174
+ $authenticationUtils = $this->get('security.authentication_utils' );
175
175
176
176
// 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
- }
177
+ $error = $authenticationUtils->getLastAuthenticationError();
187
178
188
179
// last username entered by the user
189
- $lastUsername = (null === $session) ? '' : $session->get(Security::LAST_USERNAME );
180
+ $lastUsername = $authenticationUtils->getLastUsername( );
190
181
191
182
return $this->render(
192
183
'security/login.html.twig',
193
184
array(
194
185
// last username entered by the user
195
186
'last_username' => $lastUsername,
196
- 'error' => $error,
187
+ 'error' => $error ?: '' ,
197
188
)
198
189
);
199
190
}
200
191
192
+ .. versionadded :: 2.6
193
+ The ``security.authencation_utils `` service and the
194
+ :class: `Symfony\\ Component\\ Security\\ Http\\ Authentication\\ AuthenticationUtils `
195
+ class were introduced in Symfony 2.6.
196
+
201
197
Don't let this controller confuse you. As you'll see in a moment, when the
202
198
user submits the form, the security system automatically handles the form
203
199
submission for you. If the user had submitted an invalid username or password,
0 commit comments