Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 basic GET form generated URL

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!

like image 799
AnchovyLegend Avatar asked Oct 11 '13 01:10

AnchovyLegend


2 Answers

Old question but, for people who want to know, this does the job too (Symfony 2.8) :

<?php
// src/AppBundle/Form/SearchType.php
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SearchType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod('GET')
            ->add('q', TextType::class)
            ->add('submit', SubmitType::class))
        ;
    }

    public function getBlockPrefix(){
        return '';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'csrf_protection' => false,
        ]);
    }
}

In your controller :

<?php
//...
use AppBundle\Form\SearchType;
//...
public function yourSearchAction(Request $request)
{
    $form = $this->createForm(SearchType::class);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $q = $form->get('q')->getData();
        // ...
    }
    // ...
}
like image 176
Charles-Édouard Coste Avatar answered Oct 29 '22 13:10

Charles-Édouard Coste


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') 
like image 42
hcoat Avatar answered Oct 29 '22 15:10

hcoat