加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Symfony 3在FormType中注入容器

发布时间:2020-12-13 13:20:02 所属栏目:PHP教程 来源:网络整理
导读:如何在Symfony 3.0中的FormType上注入容器? 我的services.yml文件: services: advertiser.form.report: class: AppAdvertiserBundleFormReportType arguments: ["@service_container"] 在动作控制器中: $report = $this-get( 'advertiser.form.report'
如何在Symfony 3.0中的FormType上注入容器?

我的services.yml文件:

services:
    advertiser.form.report:
        class: AppAdvertiserBundleFormReportType
        arguments: ["@service_container"]

在动作控制器中:

$report = $this->get( 'advertiser.form.report' );
$form = $this->createForm( $report );

我收到了这个错误:

Expected argument of type “string”,“AppAdvertiserBundleFormReportType” given

请改用表格工厂.

在配置中:

services:
    advertiser.form.report:
        class:  'SymfonyComponentFormForm'
        factory: ['@form.factory','create']
        arguments: [AppAdvertiserBundleFormReportType,null,{container: '@service_container'}]

在您的表单中添加容器到可能的表单选项:

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'container' => null,//The rest of options
    ]);
}

并根据需要使用它:

public function buildForm(FormBuilderInterface $builder,array $options)
{
    $container = $options['container'];
    //The rest of logic
}

在您的控制器中,只需使用表单:

$this->get('advertiser.form.report'); //It's a ready-to-use form,you don't need to call $this->createForm()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读