捆绑控制器的依赖注入
Dependency injection for a bundle controller
我有一个简单、有效的 SecurityController,我想将它变成一个包,以便我可以在我创建的几个基本网站之间共享基本身份验证功能。一切都按预期工作,直到我尝试将我的代码变成一个包。
我已经创建了我的包 class,一个 Resources/config/routing.xml 文件来声明我的登录和注销路由,在 Resources/views/Security/login.[=41 中有一个模板=] 但下面的 class 会引发错误。
<!-- 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?
在一些不同的 examples/tutorials 之后,我尝试创建一个 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>
看起来你在路由中使用 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
取决于您的服务名称
我有一个简单、有效的 SecurityController,我想将它变成一个包,以便我可以在我创建的几个基本网站之间共享基本身份验证功能。一切都按预期工作,直到我尝试将我的代码变成一个包。
我已经创建了我的包 class,一个 Resources/config/routing.xml 文件来声明我的登录和注销路由,在 Resources/views/Security/login.[=41 中有一个模板=] 但下面的 class 会引发错误。
<!-- 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?
在一些不同的 examples/tutorials 之后,我尝试创建一个 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>
看起来你在路由中使用 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
取决于您的服务名称