Symfony 6.4 - 无法将抽象表单类型声明为服务

问题描述 投票:0回答:1

在我的 Symfony 6.4 项目中,我尝试通过执行以下操作在 services.yaml 中声明抽象服务:

config/services.yaml

imports:
- { resource: services/ }
services:
# default configuration for services in *this* file
    _defaults:
       autowire: true      # Automatically injects dependencies in your services.
       autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

config/services/forms.yaml

services:
  _defaults:
    autowire: true
    autoconfigure: true

  # parent form
  App\Admin\Form\HostingFormAbstract:
    arguments:
      $authorizationChecker: '@security.authorization_checker'
      $tokenStorage: '@security.token_storage'
      $phpHandlers: '%php_handlers%'
      $nodeJsHandlers: '%nodeJs_handlers%'
    abstract:  true

  # children forms
  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 类正在从 symfony/form 包扩展 AbstractType :

namespace App\Admin\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

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;
    }
...

因此,我在使用 symfony 应用程序时收到以下错误:

The service "App\Admin\Form\HostingFormAbstract" tagged "form.type" must not be abstract.

为了了解更多背景信息,我正在将 Symfony 3.4 应用程序重写为新的 Symfony 6.4 应用程序。

它在 3.4 上运行良好,但现在看来在 services.yaml 中,您无法声明扩展 AbstractType 类的抽象服务/类。

我尝试在 yaml 中为此服务添加自定义标签,但它没有改变任何内容。

我没有在 Symfony 文档中找到任何解决方法,而且对我来说你不能这样做似乎很奇怪。也许我错过了什么。

如果有人知道如何做到这一点......

谢谢!

php symfony symfony6
1个回答
0
投票

这是

autoconfigure
选项的默认行为,将
form.type
标签设置到服务范围下的所有
FormTypeInterface
类。

但是,您可以通过设置

autoconfigure: false
来排除自动标记的任何服务:

App\Admin\Form\HostingFormAbstract:
    autoconfigure: false
    // ...

然后错误就会消失。

© www.soinside.com 2019 - 2024. All rights reserved.