我有一個捆綁包,例如,我希望能夠?qū)⑵渥⑷氲娇刂破髦?。我希望用戶要做的唯一一件事就是注冊捆綁軟件。然后可以隨意將服務(wù)注入到他們喜歡的任何地方:namespace App\Bundles;class MyService{ private $config; public function __construct(array $options) { $this->config= $options; } public function hello() { echo "Hello world"; }}我試圖在config / services.yaml中定義這一行:App\Bundles\MyService: '@bundle_service'這似乎可行,但是我不希望用戶這樣做。這是我的配置類:class Configuration implements ConfigurationInterface{ public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder("my_bundle"); $treeBuilder->getRootNode() ->children() ->arrayNode('author') ->children() ->scalarNode("name")->end() ->end() ->end() ->end(); return $treeBuilder; }}還有my_bundle配置文件,到目前為止,這只是一個測試:my_bundle: author: name: "Name"我的Bundle擴(kuò)展程序類:class MyBundleExtension extends Extension{ public function load(array $configs, ContainerBuilder $container) { $loader = new YamlFileLoader( new FileLocator(__DIR__ .'/../Resources/config') ); $loader->load('services.yaml'); $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $container->setDefinition('bundle_service', new Definition(MyService::class, [$config])); $container->setAlias(MyService::class, 'bundle_service'); }}我的捆綁課程:class MyBundle extends Bundle{ public function getContainerExtension() { return new MyBundleExtension(); }}我想念的是什么。一切正常,除非我必須定義此行App\Bundles\MyService: '@bundle_service'中config/services.yaml,我不希望我的用戶這樣做。在MyBundleExtension中,我提供了正確的定義: $container->setDefinition('bundle_service', new Definition(MyService::class, [$config])); $container->setAlias(MyService::class, 'bundle_service');當(dāng)我遺漏config/services.yaml代碼時,出現(xiàn)此錯誤:Cannot autowire service "App\Bundles\MyService": argument "$options" of method "__construct()"
- 2 回答
- 0 關(guān)注
- 158 瀏覽
添加回答
舉報
0/150
提交
取消