Symfony 6.4 无法将抽象表单类型声明为服务
Symfony 6.4 抽象表单类服务配置报错及解决办法
将继承AbstractType的抽象表单类HostingFormAbstract声明为服务时,在Symfony 6.4中出现报错,该配置从Symfony 3.4迁移而来,原版本可正常运行。
原配置文件内容
config/services.yaml
imports: - { resource: services/ } services: # default configuration for services in *this* file _defaults: autowire: true # 自动注入服务依赖 autoconfigure: true # 自动注册服务为命令、事件订阅者等
config/services/forms.yaml
services: _defaults: autowire: true autoconfigure: true # 父表单服务 App\Admin\Form\HostingFormAbstract: arguments: $authorizationChecker: '@security.authorization_checker' $tokenStorage: '@security.token_storage' $phpHandlers: '%php_handlers%' $nodeJsHandlers: '%nodeJs_handlers%' abstract: true # 子表单服务 App\Admin\Form\HostingFormCreate: parent: App\Admin\Form\HostingFormAbstract class: App\Admin\Form\HostingFormCreate calls: - [setGroupManager, ['@App\Services\Managers\GroupManager']] tags: [form.type] App\Admin\Form\HostingFormUpdate: parent: App\Admin\Form\HostingFormAbstract class: App\Admin\Form\HostingFormUpdate calls: - [setHostnameManager, ['@App\Services\Managers\HostnameManager']] tags: [form.type] ...
HostingFormAbstract类代码片段
namespace App\Admin\Form; use Symfony\Component\Form\AbstractType; // 省略use语句 abstract class HostingFormAbstract extends AbstractType { private TokenStorageInterface $tokenStorage; private AuthorizationCheckerInterface $authorizationChecker; private array $phpHandlers; private array $nodeJsHandlers; public function __construct(TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $authorizationChecker, array $phpHandlers, array $nodeJsHandlers) { $this->tokenStorage = $tokenStorage; $this->authorizationChecker = $authorizationChecker; $this->phpHandlers = $phpHandlers; $this->nodeJsHandlers = $nodeJsHandlers; } ...
运行报错
The service "App\Admin\Form\HostingFormAbstract" tagged "form.type" must not be abstract.
注:尝试添加自定义标签无效,未找到官方解决方案。
已验证的解决方案
通过以下两步修改配置可解决问题:
- 将
config/services/forms.yaml中的抽象表单服务配置移至config/services.yaml - 为
HostingFormAbstract服务添加autoconfigure: false
修改后的config/services.yaml关键配置如下:
App\Admin\Form\HostingFormAbstract: class: App\Admin\Form\HostingFormAbstract autoconfigure: false arguments: $authorizationChecker: '@security.authorization_checker' $tokenStorage: '@security.token_storage' $phpHandlers: '%php_handlers%' $nodeJsHandlers: '%nodeJs_handlers%' abstract: true # 子表单服务配置保持不变
内容的提问来源于stack exchange,提问作者CTR
相关产品推荐
相关产品推荐

