Symfony 2基本的GET表单生成的URL [英] Symfony 2 basic GET form generated URL

查看:151
本文介绍了Symfony 2基本的GET表单生成的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图用一个输入创建一个非常基本的symfony表单(用于搜索功能)。它在提交时使用GET方法。它似乎按预期工作,但它会生成一个非常丑陋和不必要的长URL。我一直在努力'清理'网址很长一段时间,我想知道是否有人遇到同样的问题,并知道如何解决它?



表格

  $ form = $ this-> createFormBuilder($ search)
- > setMethod('GET' )
- > add('q','text')
- > add('search','submit')
- > getForm();

提交表单后会生成以下网址:

  search?form [q] = red + apple& form [search] =& form [_token] = bb342d7ef928e984713d8cf3eda9a63440f973f2 

所需网址:

 搜索?q = red + apple 

预先致谢!

解决方案要创建你想要的URL,你必须使用 createNamedBuilder 来设置表单名称,你只需留空 ''
要删除 _token ,您需要将 csrf_protection 设置为false。请关注csrf保护,以确保知道关闭后会发生什么情况。



将您的代码更改为以下内容应该会给您想要的结果。 p>

  $ form = $ this-> get('form.factory') - > createNamedBuilder('','form', $ search,array(
'csrf_protection'=> false,
)) - > setMethod('GET')
- > add('q','text')
- > add('search','submit')
- > getForm();

这应该产生一个URL:

  search?q = red + apple& search = 

编辑:



如果你想摆脱& search = 可以将 search 从提交更改为按钮

   - > add('search','button')

这将需要javascript来提交表单。
下面是jQuery中的一个简单示例:

  //假设一个表单和一个按钮
$(文件).ready(function(){
$('button')。click(function(){
$('form')。submit();
});
});

这会产生一个URL:

 搜索?q = red + apple 

访问GET变量你在你的控制器中放入类似的东西:

  public function yourSearchAction(Request $ request)
{
//你的代码...

$ form-> handleRequest($ request);

if($ form-> isValid()){

$ getVars = $ form-> getData();

$ q = $ getVars ['q'];
$ page = $ getVars ['page'];
$ billing = $ em

//做某事

}

返回//您的代码

}

只是为了澄清是否要添加页面到您的URL,您需要将它添加到您的表单中:

   - > add('page','text ')


I have been trying to create an extremely basic symfony form (used for search functionality) with only one input. It uses GET method on submit. It seems to work as expected, however it generates an extremely ugly and unnecessarily long URL. I have been trying to 'clean' the URL up for a quite a while now, I was wondering if someone ran into the same problem and knows how to fix it?

Form

$form = $this->createFormBuilder($search)
            ->setMethod('GET')
            ->add('q', 'text')
            ->add('search', 'submit')
            ->getForm();

On submit the form generates the following URL:

search?form[q]=red+apple&form[search]=&form[_token]=bb342d7ef928e984713d8cf3eda9a63440f973f2

Desired URL:

search?q=red+apple

Thanks in advance!

解决方案

To create your desired URL, you will have to set the form name by using createNamedBuilder which you'll just leave blank ''. To remove _token you need to set csrf_protection to false. Please look into csrf protection to make sure you know what could happen if it is turned off.

Changing your code to the following should give you the results you want.

$form = $this->get('form.factory')->createNamedBuilder('', 'form', $search, array(
            'csrf_protection' => false,
         ))->setMethod('GET')
           ->add('q', 'text')
           ->add('search', 'submit')
           ->getForm();

This should produce a URL like:

search?q=red+apple&search=

Edit:

If you want to get rid of &search=, one way would be to change search from submit to button.

->add('search', 'button')

This will require javascript to submit your form. Here is simple example in jquery:

//This assumes one form and one button
$(document).ready(function(){
    $('button').click(function(){
        $('form').submit();
    });
});

This will produce a URL like:

search?q=red+apple

To access GET vars you put something like this in your controller:

public function yourSearchAction(Request $request)
{
    // your code ...

    $form->handleRequest($request);

    if ($form->isValid()) {

        $getVars = $form->getData();

        $q = $getVars['q'];
        $page = $getVars['page'];
        $billing = $em

        //Do something

    }

    return //your code

}

Just to clarify if you are adding page to your URL you will need to add it to your form:

->add('page', 'text') 

这篇关于Symfony 2基本的GET表单生成的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆