Symfony2 getLastUsername()函数不工作
我正在创建我的第一个Symfony2登录表单,并希望使用getLastUsername()函数设置用户名文本框的值,但调用该函数时,它只返回一个空字符串 这是我的SecurityController.php文件:Symfony2 getLastUsername()函数不工作,symfony,Symfony,我正在创建我的第一个Symfony2登录表单,并希望使用getLastUsername()函数设置用户名文本框的值,但调用该函数时,它只返回一个空字符串 这是我的SecurityController.php文件: <?php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Reques
<?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()
{
}
}
?>
以及表单的我的小枝模板:
{# 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 %}
{#app/Resources/views/security/login.html.twig}
{%extends'base.html.twig%}
{%块样式表%}
{{parent()}}
{%endblock%}
{%block javascripts%}
{{parent()}}
{%endblock%}
{%block body%}
{%if错误%}
{{error.messageKey | trans(error.messageData,'security')}
{%endif%}
输入用户名和密码以继续
{%endblock%}
一切正常。多次登录和注销,但没有显示las用户名。虽然Symfony可能正在使用cookies保存上次使用的用户名,但它没有创建任何cookies,只有PHP会话保存在cookie中。这是cookie配置中的问题还是其他问题
谢谢,它只在您提供错误的凭据时使用。哦,虽然它的工作原理与其他网站相同,但当您想要登录时,它会使用上次使用的用户名填充用户名。谢谢。你们两个应该把@malcolm的评论作为回答并接受它