简体   繁体   English

Symfony:如何从FormType中的路由获取参数?

[英]Symfony: how to get a parameter from the route within a FormType?

I'm trying to access the parameter page from the current route within a FormType . 我正在尝试从FormType的当前路由访问参数page It works in a Controller , but not in a FormType . 它适用于Controller ,但不适用于FormType I'd like to avoid passing the parameter like /?page=1 and prefer /page/1 . 我想避免传递像/?page=1和prefer /page/1

routing.yml 使用routing.yml

my_route:
    path: /data/page/{page}
    defaults:
        _controller: MyBundle:MyController:myAction

src/myBundle/Form/Type/MyFormType.php SRC / myBundle /表/类型/ MyFormType.php

class MyFormType extends AbstractType {
    // ...
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $request = Request::createFromGlobals();
        $page    = $request->query->get('page');
        echo "page: $page"; // empty

        // ...
    }
}

I'd like to avoid to pass the parameter through the Controller to the FormType and prefer to access it directly within the FormType. 我想避免将参数通过Controller传递给FormType,而是希望直接在FormType中访问它。

Any ideas? 有任何想法吗?

Thanks in advance! 提前致谢!

Edit: Regarding the selected answer; 编辑:关于选定的答案; the page attribute is accessible via $request->attributes->get('page') , not via $request->query->get('page') . page 属性可以通过$request->attributes->get('page') ,而不是通过$request->query->get('page')

I'm not sure the answer provided by Yonel is the best because you inject a dependency to your form. 我不确定Yonel提供的答案是最好的,因为你为表单注入了一个依赖项。 It has some drawbacks and the major one IMHO is that it will make the test difficult as the dependency on the page parameter is hidden. 它有一些缺点,主要的一个恕我直言,它将使测试困难,因为隐藏页面参数的依赖。

A better solution will be to add it as form option to your form. 更好的解决方案是将其作为表单选项添加到表单中。

The request object is already available in your controller and you are probably creating your form this way : 请求对象已在您的控制器中可用,您可能正在以这种方式创建表单:

$form = $this->createForm(WhateverFormType::class, $entity)

Using the createForm method, you can inject a third argument which are the options (ie additional data) you want to pass to your form. 使用createForm方法,您可以注入第三个参数,这些参数是您要传递给表单的选项(即附加数据)。

So in your controller : 所以在你的控制器中:

$page = $request->query->get('page');
$form = $this->createForm(WhateverFormType::class, $entity, ['page' => $page]);

And in your form type, follow the example given in this answer for the same question : https://stackoverflow.com/a/10922788/2721918 在您的表单类型中,请按照此答案中给出的相同问题的示例: https//stackoverflow.com/a/10922788/2721918

You need to inject the request stack service into form type to do that: 您需要将请求堆栈服务注入表单类型来执行此操作:

class MyFormType extends AbstractType 
{
    private $requestStack;

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    } 

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $request = $this->requestStack->getCurrentRequest();

        //do something: for example hide/show fields from request parameters  
    }
}

Register the form type and their dependencies: 注册表单类型及其依赖项:

services:
    app.form.type.myform:
        class: AppBundle\Form\Type\MyFormType
        arguments: ['@request_stack']
        tags:
           - { name: form.type }

However, is recommended instead to create the new option to pass all variables that you need for your form type. 但是,建议您创建新选项以传递表单类型所需的所有变量。

It is not necessary to inject the request stack service in the construct of your formtype. 没有必要在formtype的构造中注入请求堆栈服务。 You can access to the requestStack by using form event listener like that: 您可以使用表单事件侦听器访问requestStack,如下所示:

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$form->getConfig()->getRequestHandler();
// do what you need...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM