【问题标题】:Symfony 3.4 "$authenticationUtils" argumentSymfony 3.4“$authenticationUtils”参数
【发布时间】:2018-11-20 01:03:09
【问题描述】:

我需要帮助!!

这是我的security.yml

安全性:

encoders:
    UserBundle\Entity\User:
        algorithm: bcrypt
# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
    db_provider:
        entity:
            class: UserBundle:User
            property: username 

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~
        form_login:
            login_path: login
            check_path: login

我的控制器:

namespace UserBundle\Controller\Login;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class LoginController extends Controller
{
    /**
     * @Route("/login", name="login")
     */
    public function loginAction(Request $request, AuthenticationUtils $authenticationUtils)
    {
//        return $this->render('@User\Login\login.html.twig');
        $errors = $authenticationUtils->getLastAuthenticationError();
        $lastUsername = $authenticationUtils->getLastUsername();
        return $this->render('@User\Login\login.html.twig',array(
            'errors'=>$errors,
            'username'=>$lastUsername,
        ));
    }
}

我的视图(login.html.twig):

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

{% block body %}
    <div class="panel panel-success" style="width: 500px;">
        <div class="panel-heading">
            <h2>Login</h2>
        </div>
        <div class="panel-body">
            <form action="{{ path('login') }}" method="post">
                <div class="form-group">
                    <label for="username">Username : </label>
                    <input type="text" name="_username" id="username" class="form-control" value="{{ username }}">
                </div>
                <div class="form-group">
                    <label for="password">Password : </label>
                    <input type="password" name="_password" id="password" class="form-control">
                </div>
                <div class="form-group">
                    <button class="btn-info btn pull-right">Login</button>
                </div>
            </form>
        </div>
        {% if errors %}
            <div class="alert alert-danger">
                {{ errors.messageKey }}
            </div>
        {% endif %}
    </div>

{% endblock %}

错误是:

Controller "UserBundle\Controller\Login\LoginController::loginAction()" 要求您为 "$authenticationUtils" 参数提供一个值。参数可以为空且未提供空值、未提供默认值或因为在此参数之后有一个非可选参数。

我尝试搜索我没有发现问题出在哪里,即使我已经有另一个项目可以使用相同的代码正常工作

【问题讨论】:

  • 猜你没有标记你的控制器。将 services.yaml 文件与工作应用进行比较。
  • 是的,这就是问题所在,我没有像你说的那样添加 UserBundle\Controller\,谢谢你救了我 :) (y)

标签: php symfony symfony-3.4


【解决方案1】:

根据documentation

如果您收到 $authenticationUtils 参数丢失的错误, 可能是因为你需要在 Symfony 中激活这个新功能 3.4.看到这个controller service argument note

尝试在你的 services.yml 文件中定义它:

UserBundle\Controller\Login:
        public: true
        bind:
            $authenticationUtils: '@security.authentication_utils'

问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2018-11-04
    • 2018-06-17
    相关资源
    最近更新 更多