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')