Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the "Build a Login Form" article #6942

Merged
merged 1 commit into from
Sep 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 12 additions & 77 deletions security/form_login_setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ First, enable form login under your firewall:

Now, when the security system initiates the authentication process, it will
redirect the user to the login form ``/login``. Implementing this login form
visually is your job. First, create a new ``SecurityController`` inside a
bundle::
is your job. First, create a new ``SecurityController`` inside a bundle::

// src/AppBundle/Controller/SecurityController.php
namespace AppBundle\Controller;
Expand Down Expand Up @@ -139,8 +138,7 @@ configuration (``login``):

return $collection;

Great! Next, add the logic to ``loginAction`` that will display the login
form::
Great! Next, add the logic to ``loginAction`` that displays the login form::

// src/AppBundle/Controller/SecurityController.php

Expand All @@ -154,14 +152,10 @@ form::
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();

return $this->render(
'security/login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
return $this->render('security/login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}

.. versionadded:: 2.6
Expand All @@ -186,7 +180,7 @@ Finally, create the template:
.. code-block:: html+twig

{# app/Resources/views/security/login.html.twig #}
{# ... you will probably extends your base template, like base.html.twig #}
{# ... you will probably extend your base template, like base.html.twig #}

{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
Expand Down Expand Up @@ -239,13 +233,12 @@ Finally, create the template:
It may contain more information - or even sensitive information - about
the authentication failure, so use it wisely!

The form can look like anything, but has a few requirements:

* The form must POST to the ``login`` route, since that's what you configured
under the ``form_login`` key in ``security.yml``.
The form can look like anything, but it usually follows some conventions:

* The username must have the name ``_username`` and the password must have
the name ``_password``.
* The ``<form>`` element sends a ``POST`` request to the ``login`` route, since
that's what you configured under the ``form_login`` key in ``security.yml``;
* The username field has the name ``_username`` and the password field has the
name ``_password``.

.. tip::

Expand Down Expand Up @@ -385,64 +378,6 @@ fixes the problem:
array('path' => '^/', 'role' => 'ROLE_ADMIN'),
),

Also, if your firewall does *not* allow for anonymous users (no ``anonymous``
key), you'll need to create a special firewall that allows anonymous users
for the login page:

.. configuration-block::

.. code-block:: yaml

# app/config/security.yml

# ...
firewalls:
# order matters! This must be before the ^/ firewall
login_firewall:
pattern: ^/login$
anonymous: ~
secured_area:
pattern: ^/
form_login: ~

.. code-block:: xml

<!-- app/config/security.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<srv:container xmlns="http://symfony.com/schema/dic/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<config>
<!-- ... -->
<firewall name="login_firewall" pattern="^/login$">
<anonymous />
</firewall>

<firewall name="secured_area" pattern="^/">
<form-login />
</firewall>
</config>
</srv:container>

.. code-block:: php

// app/config/security.php

// ...
'firewalls' => array(
'login_firewall' => array(
'pattern' => '^/login$',
'anonymous' => null,
),
'secured_area' => array(
'pattern' => '^/',
'form_login' => null,
),
),

3. Be Sure check_path Is Behind a Firewall
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down