捆绑控制器的依赖注入 [英] Dependency injection for a bundle controller

查看:31
本文介绍了捆绑控制器的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple, working SecurityController that I would like to turn into a bundle so that I can share basic authentication functionality across a few basic websites I create. Everything is working as desired until I try to turn my code into a bundle.

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

I have created my bundle class, a Resources/config/routing.xml file to declare my login and logout routes, have a template in Resources/views/Security/login.html.twig but the following class is throwing an error.

<!-- 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" 需要构造函数参数,并且在容器中不存在.是不是忘记定义这样的服务了?

The error I am getting when I go to the login page is 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 可用于我的构造函数,但这似乎没有改变任何东西.

Following a few different examples/tutorials, I tried creating a services.xml file and loading it via DependencyInjection/EssentialSecurityExtension.php in an attempt to make AuthenticationUtils available to my constructor but that does not appear to change anything.

<!-- 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>

我缺少什么让我能够像在将代码移动到包之前那样在包内使用依赖注入?

What am I missing that allows me to use dependency injection inside a bundle as I was able to before moving this code into a bundle?

如果我只是删除对 AuthenticationUtils 的任何引用(私有属性、整个构造函数及其在 loginAction 中的用法),页面将呈现,尽管如果没有我在第一名.

If I simply remove any reference to AuthenticationUtils (the private property, the entire constructor, and its usage inside loginAction) the page renders, though it will not function as desired without that last authentication error that I am using AuthenticationUtils for in the first place.

旁注,如果我手动添加 JustinVoelker\EssentialSecurityBundle\Controller\SecurityController: ~ 到我的应用程序主 config/services.xml 文件,控制器错误消失得如此明显,我在我的捆绑以完成这项工作.

Side note, if I manually add JustinVoelker\EssentialSecurityBundle\Controller\SecurityController: ~ to my applications main config/services.xml file, The controller error disappears so clearly I'm missing something inside my bundle to make this work.

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

Perhaps there is another way to achieve my ultimate end goal of returning the last authentication error message to the login page, but my question is what am I missing that is preventing this dependency injection from working as it did before I bundled my controller and as it appears to work in so many examples I've seen.

EDIT 2019-05-30 包括我原来的routing.xml 的一部分

EDIT 2019-05-30 Including a portion of my original routing.xml

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

推荐答案

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

Looks like in your routing you use JustinVoelker\EssentialSecurityBundle\Controller\SecurityController but your service name is essential_security.controller You should either change your routing or service definition

您可以添加别名

<!-- 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参数)

or just rename it (note you can omit class parameter)

<!-- 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

取决于您的服务名称

这篇关于捆绑控制器的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆