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!
Old question but, for people who want to know, this does the job too (Symfony 2.8) :
In your controller :
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 setcsrf_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.
This should produce a URL like:
Edit:
If you want to get rid of
&search=
, one way would be to changesearch
fromsubmit
tobutton
.This will require javascript to submit your form. Here is simple example in jquery:
This will produce a URL like:
To access GET vars you put something like this in your controller:
Just to clarify if you are adding
page
to your URL you will need to add it to your form: