![](/img/trans.png)
[英]InvalidConfigurationException with custom bundle in Symfony6.2
[英]Custom Bundle with Symfony 6.2
我正在嘗試創建一個 Bundle 以在我的不同項目之間共享一些服務和實用程序類。 我在使用 Symfony 5.4 時已經這樣做了。 但現在我想遷移到PHP 8.1使用Symfony 6.2 。 我不知道我做錯了什么,但我的 web 項目只是看不到我在 Bundle 中創建的服務。
一步步:
symfony new my-webapp --version="6.2.*" --webapp
{
"name": "carlospauluk/my-bundle",
"type": "project",
"license": "proprietary",
"require": {
"php": ">=8.1"
},
"require-dev": {
"symfony/http-kernel": "6.2.*"
},
"autoload": {
"psr-4": {
"MyBundleNamespace\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"MyBundleNamespace\\Tests\\": "tests/"
}
}
}
在 config/services.yaml 中,我將其更改為:
parameters:
services:
_defaults:
autowire: true
autoconfigure: true
MyBundleNamespace\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
所以我在 src/Service 中創建了我的服務:
<?php
namespace MyBundleNamespace\Service;
class NumberGeneratorService
{
public function generate(int $max) {
return random_int(0, $max);
}
}
顯然我的服務在 Bundle 中配置正確,對吧? 之后,在我的 my-webapp 中,在 composer.json 中,我添加了 my-bundle 文件夾的本地存儲庫:
,
"repositories": [
{
"type": "path",
"url": "../my-bundle"
}
]
進而...
composer require "carlospauluk/my-bundle @dev"
好的,我希望一切都已經在工作,並且我的 NumberGeneratorService 服務已經在 my-webapp 中可用。 但不是。 當我跑步時:
php bin/console debug:container MyBundleNamespace
它返回:
No services found that match "MyBundleNamespace".
可能缺少什么?
當我使用 Symfony 5.4 在我的項目中設置我的包時,我不記得做了什么與此有太大不同的事情。
請問你能幫幫我嗎?
兩個代碼都在這里:
https://github.com/carlospauluk/my-bundle
https://github.com/carlospauluk/my-webapp
謝謝
最后,經過將近 3 天的嘗試,我想出了應該怎么做。
我需要在 /src/MyBundle.php 中實現 loadExtension 方法
<?php
namespace MyBundleNamespace;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
class MyBundle extends AbstractBundle
{
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
$container->import(__DIR__ . '/Resources/config/services.xml');
}
}
並且還必須創建 /src/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="my_bundle_namespace.service.number_generator_service"
class="MyBundleNamespace\Service\NumberGeneratorService">
</service>
<service id="MyBundleNamespace\Service\NumberGeneratorService" public="true"
alias="my_bundle_namespace.service.number_generator_service"/>
</services>
</container>
沒有這個,它是行不通的。
Symfony 框架非常出色,但不幸的是它的文檔非常薄弱並且在某些地方令人困惑。
例如,在https://symfony.com/doc/current/bundles.html上,他們說“這個空的 class 是創建新包所需的唯一部分。”,我們清楚地看到這是不正確的。 如果不實現 loadExtension 方法,我們就無法訪問 bundle 中的任何服務。 如果他們說清楚了,那就容易多了。 Symfony 的文檔過於全面,盡管在某些方面非常膚淺。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.