调用 createForm 时未找到服务 "form.factory"

Service "form.factory" not found when calling createForm

我遵循 class,其中有一个如何制作表格的示例。当我尝试它时,出现 ServiceNotFoundException 错误,而且我在网络上的任何地方都找不到它。

Service "form.factory" not found: the container inside "App\Controller\MyTestController" is a smaller service locator that only knows about the "doctrine", "http_kernel", "parameter_bag", "request_stack", "router", "session" and "twig" services.

当我查看文档时,我遵循了每一步并制作了一个虚拟示例,但我仍然遇到错误。 https://symfony.com/doc/current/best_practices/forms.html

当我调用 Controller.php 函数 createForm():

时出现此错误
public function add($id, Request $request)
{
    $ads = new Ads();
    $form = $this->createForm(AdsType::class, $ads);
}

我在我的整个项目中使用 Ctrl-F "form.factory" 别名,它看起来已经设置好了。

进入ControllerTrait的函数:

    C:\Users\Marcel\Documents\Projects\PHP\Symfony\OpenClassRoom\vendor\symfony\framework-bundle\Controller\ControllerTrait.php:
  314      protected function createForm(string $type, $data = null, array $options = []): FormInterface
  315      {
  316:         return $this->container->get('form.factory')->create($type, $data, $options);
  317      }
  318  
  ...
  324      protected function createFormBuilder($data = null, array $options = []): FormBuilderInterface
  325      {
  326:         return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
  327      }

并且在 AbstractController 中:

C:\Users\Marcel\Documents\Projects\PHP\Symfony\OpenClassRoom\vendor\symfony\framework-bundle\Controller\AbstractController.php:
       84              'twig' => '?'.Environment::class,
       85              'doctrine' => '?'.ManagerRegistry::class,
       86:             'form.factory' => '?'.FormFactoryInterface::class,
       87              'security.token_storage' => '?'.TokenStorageInterface::class,
       88              'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class,

它在 Symfony 4.2 中仍然有效吗?

它没有被弃用,但自 Symfony 4.0 或 4.1 以来默认情况下并未安装,因此您现在需要使用 composer 安装它。

composer require symfony/form