【发布时间】:2019-05-30 02:11:38
【问题描述】:
我有一个简单的、可工作的 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 中的用法),页面将呈现,但如果没有我在第一名。
旁注,如果我手动将 JustinVoelker\EssentialSecurityBundle\Controller\SecurityController: ~ 添加到我的应用程序主 config/services.xml 文件中,控制器错误就会消失得如此明显,我在我的包中缺少一些东西来完成这项工作。
也许还有另一种方法可以实现我的最终目标,即将最后一个身份验证错误消息返回到登录页面,但我的问题是我缺少什么阻止这种依赖注入像捆绑控制器之前那样工作并且它似乎在我见过的许多例子中都有效。
EDIT 2019-05-30包括我原来的routing.xml的一部分
<route id="essential_security_login" path="/login">
<default key="_controller">EssentialSecurityBundle:Security:login</default>
</route>
【问题讨论】:
-
重要问题:您使用什么版本?你添加了 _autowire: default 吗?我写了关于 Symfony 3.3+ 的语法来做到这一点 - tomasvotruba.cz/blog/2017/05/07/…
-
你能分享那个控制器的路由吗?