首页 > 解决方案 > 捆绑控制器的依赖注入

问题描述

我有一个简单、有效的 SecurityController,我想将它变成一个包,以便我可以在我创建的几个基本网站之间共享基本身份验证功能。一切都按预期工作,直到我尝试将我的代码变成一个包。

我已经创建了我的包类,一个 Resources/config/routing.xml 文件来声明我的登录和注销路由,在 Resources/views/Security/login.html.twig 中有一个模板,但是下面的类抛出了一个错误。

<!-- Controller/SecurityController.php -->
<?php

namespace JustinVoelker\EssentialSecurityBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends AbstractController
{
    private $authenticationUtils;

    public function __construct(AuthenticationUtils $authenticationUtils)
    {
        $this->authenticationUtils = $authenticationUtils;
    }

    public function loginAction()
    {
        $error = $this->authenticationUtils->getLastAuthenticationError();

        return $this->render('@EssentialSecurity/Security/login.html.twig', [
            'error' => $error,
        ]);
    }

    ... Comments and additional functions removed for simplicity

}

我进入登录页面时遇到的错误是Controller "JustinVoelker\EssentialSecurityBundle\Controller\SecurityController" has required constructor arguments and does not exist in the container. Did you forget to define such a service?

在几个不同的示例/教程之后,我尝试创建一个 services.xml 文件并通过 DependencyInjection/EssentialSecurityExtension.php 加载它,以尝试使 AuthenticationUtils 可用于我的构造函数,但这似乎没有改变任何东西。

<!-- DependencyInjection/EssentialSecurityExtension.php -->
<?php

namespace JustinVoelker\EssentialSecurityBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

class EssentialSecurityExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new XmlFileLoader(
            $container,
            new FileLocator(__DIR__.'/../Resources/config')
        );
        $loader->load('services.xml');
    }
}
<!-- Resources/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="essential_security.controller"
                 class="JustinVoelker\EssentialSecurityBundle\Controller\SecurityController">
            <argument type="service" id="security.authentication_utils"/>
        </service>
    </services>
</container>

我错过了什么,让我可以在将代码移动到包中之前在包中使用依赖注入?

如果我只是删除对 AuthenticationUtils 的任何引用(私有属性、整个构造函数以及它在 loginAction 中的用法),页面就会呈现,尽管如果没有我首先使用 AuthenticationUtils 的最后一个身份验证错误,它将无法按预期运行。

旁注,如果我手动添加JustinVoelker\EssentialSecurityBundle\Controller\SecurityController: ~到我的应用程序主 config/services.xml 文件中,控制器错误就会消失得很清楚,我在我的包中缺少一些东西来使它工作。

也许还有另一种方法可以实现我将最后一个身份验证错误消息返回到登录页面的最终目标,但我的问题是我错过了什么阻止这种依赖注入像我捆绑控制器之前那样工作似乎在我见过的很多例子中都有效。

编辑 2019-05-30包括我原始 routing.xml 的一部分

<route id="essential_security_login" path="/login">
    <default key="_controller">EssentialSecurityBundle:Security:login</default>
</route>

标签: symfonysymfony4

解决方案


看起来在您使用的路由中,JustinVoelker\EssentialSecurityBundle\Controller\SecurityController但您的服务名称是essential_security.controller 您应该更改您的路由或服务定义

您可以添加别名

<!-- Resources/config/services.xml -->
<service id="JustinVoelker\EssentialSecurityBundle\Controller\SecurityController" alias="essential_security.controller"/>

<service id="essential_security.controller" class="JustinVoelker\EssentialSecurityBundle\Controller\SecurityController">
    <argument type="service" id="security.authentication_utils"/>
    <tag name="controller.service_arguments"/>
</service>

或者只是重命名它(注意你可以省略class参数)

<!-- Resources/config/services.xml -->
<service id="JustinVoelker\EssentialSecurityBundle\Controller\SecurityController">
    <argument type="service" id="security.authentication_utils"/>
    <tag name="controller.service_arguments"/>
</service>

或在您的路由中:

route_name:
    path:     /path
    controller: JustinVoelker\EssentialSecurityBundle\Controller\SecurityController::loginAction
route_name:
    path:     /path
    controller: essential_security.controller::loginAction

取决于您的服务名称


推荐阅读