gpt4 book ai didi

php - 在 Symfony 中注册特定的表单类型 - Compiler Pass

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

我已经创建了一个特定的表单类型:OrganizationsType。此类型在构造函数中采用 3 个参数。 tokenStorage (TokenStorageInterface)、Router(Router) 和我的类中的一个接口(interface)对象 (PreparatorInterface)。这个表单在一个 bundle 中,我希望每个人都通过实现 PreparatorInterface 来创建自己的 Preparator

所以我想创建一个 Compiler pass,它可以用所有参数注册这个 FormType。我试试这个:

$organizationListPreparator = $container->findTaggedServiceIds(self::TAG);

if (empty($organizationListPreparator)) {
throw new \Exception('CoffreoProOrganizationSelectorBundle need a preparator. Check README.');
}

$container->register(OrganizationsForm::class, OrganizationsForm::class)
->addArgument(new Reference('security.token_storage'))
->addArgument(new Reference(key($organizationListPreparator)))
->addArgument(new Reference('router'))
->setAutoconfigured(true)
->setAutowired(true)
->setAbstract(true)
->addTag('form.type');

但是当我尝试实例化一个这样的表单时

$organizationsForm = $this->createForm(OrganizationsForm::class);

我该怎么做?

编辑

我收到此错误消息:

Too few arguments to function OrganizationSelectorBundle\Form\OrganizationsForm::__construct(), 0 passed in /var/www/myProject/vendor/symfony/form/FormRegistry.php on line 92 and exactly 3 expected

[2018-12-18 16:34:38] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Too few arguments to function OrganizationSelectorBundle\Form\OrganizationsForm::__construct(), 0 passed in /var/www/myProject/vendor/symfony/form/FormRegistry.php on line 92 and exactly 3 expected" at /var/www/myProject/vendor/organization-selector-bundle/Form/OrganizationsForm.php line 58 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Too few arguments to function OrganizationSelectorBundle\\Form\\OrganizationsForm::__construct(), 0 passed in /var/www/myProject/vendor/symfony/form/FormRegistry.php on line 92 and exactly 3 expected at /var/www/myProject/vendor/organization-selector-bundle/Form/OrganizationsForm.php:58)"} []

编辑

当我启动 bin/console deb:container OrganizationForm

服务“OrganizationSelectorBundle\Form\OrganizationsForm”的信息


期权值(value)


服务 ID OrganizationSelectorBundle\Form\OrganizationsForm
类 OrganizationSelectorBundle\Form\OrganizationsForm
标签 form.type
公开号
合成号
懒惰不
共享是
摘要号
Autowiring 是
自动配置是


当我添加 dump('Did I pass here') 时,我传递了我的编译器传递;死();

当我启动 bin/console debug:container --tag form.type 以列出所有带有标签 form.type 的服务时

Symfony Container Services Tagged with "form.type" Tag
======================================================

--------------------------------------------------------------- ------- ---------------------------------------------------------------
Service ID alias Class name
--------------------------------------------------------------- ------- ---------------------------------------------------------------
App\Form\StaffingCustomerType App\Form\StaffingCustomerType
App\Form\UserType App\Form\UserType
OrganizationSelectorBundle\Form\OrganizationsForm OrganizationSelectorBundle\Form\OrganizationsForm
--------------------------------------------------------------- ------- ---------------------------------------------------------------

最佳答案

我找到了比使用 CompilerPass 更好的解决方案。

我直接在 yaml 中使用服务定义,并将特定服务作为第二个参数传递。然后我使用别名为此服务 ID 提供默认服务:

OrganizationSelectorBundle\Form\OrganizationsForm:
arguments:
- '@security.token_storage'
- '@coffreo_organization_bundle.organization_list_preparator'
- '@router'
tags: [form.type]
public: false

coffreo_organization_bundle.organization_list_preparator:
class: Coffreo\Pro\OrganizationSelectorBundle\Preparator\AllOrganizationListPreparator
arguments: ['@translator']
public: false

关于php - 在 Symfony 中注册特定的表单类型 - Compiler Pass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53835798/

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