Symfony的错误登录
问题描述:
在Symfony的,我想创建一个loginpage,但我得到了以下错误:Symfony的错误登录
Variable "error" does not exist in QuizBundle:Default:welcome.html.twig at line 37
有人可以帮助我? 这里是我的代码:
类:
class SecurityController extends Controller
{
public function indexAction($name)
{
return $this->render('', array('name' => $name));
}
/**
* @Route("/welcome", name="welcome")
*/
public function welcomeAction(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('@Quiz/Default/welcome.html.twig', array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
}
嫩枝文件:
<div id="loginfields" style="display: none">
{% if error %}
<div>{{error.messageKey|trans(error.messageData, 'security')}}</div>
{% endif %}
<form class="form-signin" action="{{path('welcome')}}" method="post">
<h2 class="form-signin-heading">Please sign in</h2>
<label class="sr-only" for="username">Username:</label>
<input type="text" id="username" placeholder="Username" class="form-control" name="_username" value="{{ last_username }}"/>
<label class="sr-only" for="password">Password:</label>
<input type="password" id="password" class="form-control" placeholder="Password" name="_password" />
<input type="hidden" name="_target_path" value="/admin" />
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
Security.yml
security:
encoders:
QuizBundle\Entity\User: plaintext
providers:
our_db_provider:
entity:
class: QuizBundle:User
firewalls:
main:
anonymous: ~
form_login:
login_path: welcome
check_path: welcome
答
要使用error
变量(或避免异常/错误,当您尝试使用也许不会存在的变量),你需要检查它是否存在这样的:在define Twig documentation
{% if error is defined %}
<div>{{error.messageKey|trans(error.messageData, 'security')}}</div>
{% endif %}
更多的相关信息。
对不起,延迟回复。问题是我有一个login.html.twig文件,它与我的问题中发布的文件完全相同,但在这个文件中我有'
不要紧,从我的角度来看,这是一个愚蠢的错误,因为我在另一个控制器的其他地方有/欢迎路径。 – Otonel