Symfony2 getLastUsername()函数不起作用

时间:2015-09-02 00:47:46

标签: symfony

我正在创建我的第一个Symfony2登录表单,并希望使用getLastUsername()函数来设置用户名文本框的值,但在调用该函数时,它只返回一个空字符串。

这是我的SecurityController.php文件:

<?php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class SecurityController extends Controller
{
    /**
     * @Route("/login", name="login")
     */
    public function loginAction(Request $request)
    {
        $authenticationUtils = $this->get('security.authentication_utils');

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();

        // 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,
            )
        );
    }

    /**
     * @Route("/login_check", name="login_check")
     */
    public function loginCheckAction()
    {
    }
}
?>

和我的表格的Twig模板:

{# app/Resources/views/security/login.html.twig #}
{% extends 'base.html.twig' %}

{% block stylesheets %}
    {{ parent() }}
    <link href="{{ asset('css/login.css') }}" rel="stylesheet" />
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script src="{{ asset('js/login.js') }}"></script>
{% endblock %}

{% block body %}
    <div id="container">
    {% if error %}
        <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
    {% endif %}
        <div id="loginbox">
            <form id="loginform" action="{{ path('login_check') }}" method="post">
                <p>Enter username and password to continue.</p>
                <div class="input-group input-sm">
                    <span class="input-group-addon"><i class="fa fa-user"></i></span><input class="form-control" type="text" name="_username" id="username" placeholder="Username" value="{{ last_username }}" />
                </div>
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-lock"></i></span><input class="form-control" type="password" name="_password" id="password" placeholder="Password" />
                </div>
                <div class="form-actions clearfix">
                    <div class="pull-left">
                        <a href="#registerform" class="flip-link to-register blue">Create new account</a>
                    </div>
                    <div class="pull-right">
                        <a href="#recoverform" class="flip-link to-recover grey">Lost password?</a>
                    </div>
                    <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}" />
                    <input type="submit" class="btn btn-block btn-primary btn-default" value="Login" />
                </div>
            </form>
        </div>
    </div>
{% endblock %}

一切正常。登录和退出多次但没有显示las用户名。思想Symfony可能正在使用cookie来保存上次使用的用户名,但它没有创建任何cookie,只有PHP会话保存在cookie中。这是cookie配置中的问题还是可能是其他问题?

谢谢,

0 个答案:

没有答案