Symfony logo

Login

Choose between two default users: user/userpass (ROLE_USER) or admin/adminpass (ROLE_ADMIN)

Code behind this page

Controller Code

    /**
     * @Route("/login", name="_demo_login")
     * @Template()
     */
    public function loginAction()
    {
        if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
        }

        return array(
            'last_username' => $this->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
            'error'         => $error,
        );
    }

Template Code

{% extends 'AcmeDemoBundle::layout.html.twig' %}

{% block content %}
    <h1>Login</h1>

    <p>
        Choose between two default users: <em>user/userpass</em> <small>(ROLE_USER)</small> or <em>admin/adminpass</em> <small>(ROLE_ADMIN)</small>
    </p>

    {% if error %}
        <div class="error">{{ error.message }}</div>
    {% endif %}

    <form action="{{ path("_security_check") }}" method="post" id="login">
        <div>
            <label for="username">Username</label>
            <input type="text" id="username" name="_username" value="{{ last_username }}" />
        </div>

        <div>
            <label for="password">Password</label>
            <input type="password" id="password" name="_password" />
        </div>

        <input type="submit" class="symfony-button-grey" value="LOGIN" />
    </form>
{% endblock %}