Symfony-在捆绑包内创建自己的配置service.yaml

问题描述

我正在尝试很多时间,但也许甚至不可能。 对不起,我的语言不好。 因此,我跟随Symfony Doc https://symfony.com/doc/current/bundles.html创建了新的Bundle,然后我跟随https://symfony.com/doc/current/bundles/extension.html创建了DI扩展。

我的文件: AcmeHelloExtension.php

namespace App\Acme\HelloBundle\DependencyInjection;

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

class AcmeHelloExtension extends Extension
{

    public function load(array $configs,ContainerBuilder $container)
    {
        $loader = new XmlFileLoader(
            $container,new FileLocator(__DIR__.'/../Resources/config')
        );
        $loader->load('services.xml');
    }
}

AcmeHelloBundle.php


namespace App\Acme\HelloBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeHelloBundle extends Bundle
{

}

我将其添加到config / bundles.php

src / Acme / HelloBundle / Resources / config / services.yaml

services:
    App\Acme\HelloBundle\AcmeHelloBundle:
        tags: ['example-tags']

此服务文件未自动加载,我是否需要执行下一步或应该工作?当我用debug:container .... bundle检查它时... 选项标签的值为空。当我将这段代码放在config / services.yaml中时,它可以工作。

解决方法

基本问题是捆绑软件的源代码位于项目的src目录下:

project
    src
        Ztest
            ZtestBundle.php

这反过来导致捆绑包的服务通过应用程序的config / services.yaml文件自动连接:

# project\config\services
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            ...
            - '../src/Ztest' # Add this to fix the problem

不包括捆绑软件的源代码,可以解决此问题。在应用程序级别进行自动布线将覆盖在捆绑级别进行的所有手动布线。

当然,通常如果您确定需要捆绑软件,则其源代码应位于其自己的独立目录中:

project
    src
    src-ztest-bundle

要执行此操作,您还需要更新composer.json的psr-4部分并运行“ composer dump-autoload”。

请记住,在Symfony 4+中,唯一推荐的自定义捆绑包用法是在多个Symfony应用程序之间共享的代码。在这种情况下,捆绑软件最终应最终位于其自己的存储库和composer软件包中。

但是,仍然支持应用程序内的自定义捆绑包,有时它们会有用。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...