Symfony 4 - 来自自定义捆绑包的服务不起作用
Posted
技术标签:
【中文标题】Symfony 4 - 来自自定义捆绑包的服务不起作用【英文标题】:Symfony 4 - services from custom bundle not working 【发布时间】:2019-12-13 17:48:48 【问题描述】:我将 symfony 3 迁移到 symfony 4.3,并且我的自定义包移动到:src/Bundles/FooBundle。
结构 FooBundle: - 依赖注入 - 配置.php - FooExtension.php - 事件 - 模型 - 资源 - 配置 - 服务.yml - 服务 - 树枝 - 例外 - FooBundle.php
还有文件 Bundles/FooBundle/Resources/config/servies.yaml
services:
foo:
class: App\Bundles\FooBundle\Service\Foo
arguments:
- '%foo.argument1%'
- '%foo.argument2%'
- '%foo.argument3%'
foo.listener.example:
class: App\Bundles\FooBundle\Event\Listener\ExampleListener
arguments: ['@annotations.reader']
tags:
- name: kernel.event_listener, event: kernel.controller, method: onKernelController
foo.filter:
class: App\Bundles\FooBundle\Filter\FilterConverter
tags:
- name: request.param_converter
Bundles/FooBundle/DependencyInjection/Configuration.php
<?php
namespace App\Bundles\FooBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
/**
* @inheritdoc
*/
public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder('foo');
$treeBuilder->getRootNode()
->children()
->scalarNode('argument1')->isRequired()->end()
->scalarNode('argument2')->isRequired()->end()
->scalarNode('argument3')->isRequired()->end()
->end();
return $treeBuilder;
Bundles/FooBundle/DependencyInjection/FooExtension.php
<?php
namespace App\Bundles\FooBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class FooExtension extends Extension
/**
* @inheritdoc
*/
public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('foo.argument1', $config['page_field_name']);
$container->setParameter('foo.argument2', $config['per_page_field_name']);
$container->setParameter('foo.argument3', $config['sort_field_name']);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
并捆绑注册:config/bundles.php
App\Bundles\FooBundle\FooBundle::class => ['all' => true],
配置包:
config/packages/foo.yam
foo:
argument1: test1
argument2: test2
argument3: test3
完成...运行应用程序并出现错误: 无法自动装配服务“App\Bundles\FooBundle\Service\Foo”:方法“__construct()”的参数“$argument1”没有类型提示,您应该明确配置其值。
但是,当我在 config/services.yaml 中添加 conf 时:
App\Bundles\FooBundle\Service\Foo:
arguments:
- '%foo.argument1%'
- '%foo.argument2%'
- '%foo.argument3%'
那个工作...
问题:为什么捆绑服务不起作用?
【问题讨论】:
【参考方案1】:我猜问题是 symfony 中的默认配置为 App 命名空间下的所有类添加了自动装配。
查看config/services.yml
下的配置:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/DependencyInjection,Entity,Migrations,Tests,Kernel.php'
您可以删除这些行:
App\:
resource: '../src/*'
exclude: '../src/DependencyInjection,Entity,Migrations,Tests,Kernel.php'
问题应该会消失。
【讨论】:
以上是关于Symfony 4 - 来自自定义捆绑包的服务不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何降级 symfony2 捆绑包 knp_compnent?
ClassNotFoundException - 如何在 symfony (2.4.4) 中注册自定义命名空间