gpt4 book ai didi

php - 调用 createForm 时找不到服务 "form.factory"

转载 作者:搜寻专家 更新时间:2023-10-31 21:19:54 25 4
gpt4 key购买 nike

我学习了一个类(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

关于php - 调用 createForm 时找不到服务 "form.factory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54989464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com