[英]Overriding Controller in Symfony 4.4 (Decorates) - “You have requested a non-existent service”
I have a Bundle named Jason/JasonSticksBundle and inside this I have a controller named StickController.我有一个名为 Jason/JasonSticksBundle 的 Bundle,在其中我有一个名为 StickController 的 controller。
I have a service defined within Jason/JasonSticksBundle/Resources/config/services.yaml我在 Jason/JasonSticksBundle/Resources/config/services.yaml 中定义了一个服务
Jason\JasonSticksBundle\Controller\StickController:
public: true
arguments: []
tags: ['controller.service_arguments']
My services.yaml is loaded via JasonSticksExtension->load(), and my bundle is registered in bundles.php.我的 services.yaml 是通过 JasonSticksExtension->load() 加载的,我的包在 bundles.php 中注册。
The bundle works fine as-is.该捆绑包按原样工作。
BUT但
For this particular project I want to override some methods within the StickController, so I am trying to "decorate" it.对于这个特定的项目,我想覆盖 StickController 中的一些方法,所以我试图“装饰”它。
At the bottom of my regular services.yaml therefore I have the following:在我的常规服务的底部。yaml 因此我有以下内容:
App\Controller\StickController:
decorates: Jason\JasonSticksBundle\Controller\StickController
but I get this error但我得到这个错误
You have requested a non-existent service "Jason\JasonSticksBundle\Controller\StickController".您请求了一个不存在的服务“Jason\JasonSticksBundle\Controller\StickController”。
Finally, if I run最后,如果我跑
php bin/console debug:container
I can see my service我可以看到我的服务
Jason\JasonSticksBundle\Controller\StickController Jason\JasonSticksBundle\Controller\StickController
All I can imagine is that my services.yaml is being registered before my StickBundle is getting registered, thus it doesn't exist at the right time.我所能想象的只是我的 services.yaml 在我的 StickBundle 注册之前被注册,因此它在正确的时间不存在。 But I don't know how to change the ordering of this, or if that would even be a viable option?
但我不知道如何改变这个顺序,或者这是否是一个可行的选择?
Any help appreciated.任何帮助表示赞赏。
I have found a workaround, though not solved.我找到了解决方法,但没有解决。 This definitely is not the way to do it - decorate exists for a reason, and I am unsure why I can't get that to work so any advice on that would still be appreciated.
这绝对不是这样做的方法 - 装饰的存在是有原因的,我不确定为什么我不能让它工作,所以任何关于它的建议仍然会受到赞赏。
For my workaround:对于我的解决方法:
I removed my controller definitions from my Bundles services.yaml, and I edited my ConfigTreeBuilder of my bundles Configuration class to include a scalarNode field to allow a class name to be passed - this class name will be the controller to override the bundles. I removed my controller definitions from my Bundles services.yaml, and I edited my ConfigTreeBuilder of my bundles Configuration class to include a scalarNode field to allow a class name to be passed - this class name will be the controller to override the bundles.
Then, to register the controller services without using services.yaml I modified my JasonStickExtension class, and after loading the services.yaml file (still needed for other services) I added the following: Then, to register the controller services without using services.yaml I modified my JasonStickExtension class, and after loading the services.yaml file (still needed for other services) I added the following:
$stickControllerDefinition = $container->findDefinition('Jason\JasonSticksBundle\Controller\StickController');
$stickControllerDefinition->addTag("controller.service_arguments");
$stickDec = $config['stick_controller_class'];
if($stickDec!=null) {
$stickControllerDefinition->setClass($stickDec);
}
This is a way direct way of doing it, and as I say I really don't think it's the proper way.这是一种直接的方式,正如我所说,我真的不认为这是正确的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.