Symfony 2 basic format GET generated URL

I am trying to create an extremely basic symfony form (used for a search function) with only one input. It uses the GET method to send. It seems to work as expected, however it generates an extremely ugly and overly long URL. I’ve been trying to "clear" the URL for a long time, I was wondering if someone ran into the same problem and knows how to fix it?

The form

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

When you submit the form, the following URL is created:

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

Desired URL:

 search?q=red+apple 

Thanks in advance!

+7
url get forms symfony formbuilder
source share
2 answers

To create the desired URL, you will need to set the form name using createNamedBuilder , which you simply leave blank. '' To remove _token , you need to set csrf_protection to false. Please review csrf protection to make sure you know what might happen if it is disabled.

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

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

This should result in 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') 

You will need javascript for this. Here is a simple example in jquery:

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

This will produce a URL, for example:

 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 } 

To clarify if you are adding page to your URL, you need to add it to your form:

 ->add('page', 'text') 
+16
source share

An old question, but for people who want to know, this also does the job (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(); // ... } // ... } 
+1
source share

All Articles