无法将参数传递给symfony 4中的树枝模板

时间:2018-10-18 11:31:23

标签: php twig symfony4

我很难将2个参数从控制器传递到树枝模板以呈现表单。 Symfony不断告诉我“不存在可变错误。” 现在,经过测试并确认,即使在控制器中定义的变量“错误” “ last_username” 也未到达模板! 只是不明白为什么...甚至遍历了我所有的配置文件...

具有讽刺意味的是,我也在另一个迷你应用程序中使用了相同的代码,并且效果很好!

我想念什么?

控制器代码:

<?php 

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController
{   
    /**
     * @var \Twig_Environment
     */
    private $twig;

    public function __construct(\Twig_Environment $twig)
    {
        $this->twig = $twig;
    }

    /**
     * @Route("/login", name="security_login")
     */
    public function login(AuthenticationUtils $authenticationUtils)
    {
        return new Response($this->twig->render(
            'security/login.html.twig',
            [
                'last_username'=> $authenticationUtils->getLastUsername(),
                'error'=> $authenticationUtils->getLastAuthenticationError(),
            ]
         ));
    }

    /**
     * @Route("/logout", name="security_logout")
     */
    public function logout()
    {
        # code...
    }
}

树枝模板:

{% extends 'base.html.twig' %}

{% block body %}

    {% if  error %}
        <div class="alert alert-danger">
            {{ error.messageKey|trans(error.messageData, 'security') }}
        </div>
    {% endif %}

    <div class="container">
        <form action="{{ path('security_login') }}" method="post">
            <div class="form-group">
                <label class="form-control-label" for="username">User Name</label>
                <input type="email" class="form-control" id="username" name="_username" required="required"  value="{{ last_username }}" aria-describedby="user name">
                <small id="userNameHelp" class="form-text text-muted">This will be your login name!.</small>
            </div>

            <div class="form-group">
                <label class="form-control-label required" for="password">Password</label>
                <input type="password" class="form-control" id="password" name="_password" required="required" placeholder="Password">
            {# </div>
                <div class="form-group form-check">
                <input type="checkbox" class="form-check-input" id="exampleCheck1">
                <label class="form-check-label" for="exampleCheck1">Im aMuppet and forgot my password...</label>
            </div> #}
        <div class="form-group">
            <button type="submit" class="btn btn-primary" id="Login" name="Login">Submit</button>
        </div>

        <input type="hidden" name="_csrf_token"  value="{{ csrf_token('authenticate') }}">
        <input type="hidden" name="_target_path" value="{{ app.request.get('redirect_to') }}">>

        </form>
    </div>

{% endblock %}

0 个答案:

没有答案