Php 在Syfmony-编译器过程中注册特定的表单类型
我创建了一个特定的表单类型:Php 在Syfmony-编译器过程中注册特定的表单类型,php,symfony,Php,Symfony,我创建了一个特定的表单类型:OrganizationsType。此类型在构造函数中接受3个参数。令牌存储(TokenStorageInterface)、路由器(Router)和我的类(PreparatorInterface)中的接口对象。这个表单在一个包中,我希望每个人通过实现PreparatorInterface来创建自己的Preparator 所以我想创建一个编译器过程,可以用所有参数注册这个FormType。我试试这个: $organizationListPreparator = $con
OrganizationsType
。此类型在构造函数中接受3个参数。令牌存储(TokenStorageInterface
)、路由器(Router
)和我的类(PreparatorInterface
)中的接口对象。这个表单在一个包中,我希望每个人通过实现PreparatorInterface
来创建自己的Preparator
所以我想创建一个编译器过程,可以用所有参数注册这个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”的信息
期权价值
服务ID OrganizationSelectorBundle\Form\OrganizationsForm
类组织SelectorBundle\Form\OrganizationsForm
标签表单类型
公共编号
合成no
懒惰不
共享是
摘要否
自动连线是
自动配置“是”
当我添加dump时,我传递了我的编译器传递(“我在这里传递了吗”);模具() 当我启动
bin/console debug:container--tag form.type
以列出带有tagform.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
考虑用错误消息的相关部分更新你的问题(如果有的话)。是否已将OrganizationsForm从autowire中排除?你为什么把它抽象化?看起来你根本不需要这个,只要你要求“每个人”都有别名PreparatorInterface.Good。现在,使用“bin/console debug:container”验证您没有OrganizationsForm服务,并验证是否正在使用简单的die语句调用编译器传递代码。