title: How to make a Symfony GET form redirect to route with parameter?
body: <p>
  I want to create a form for searching a profile by username which redirect then to the profile page of the user. Btw, I use Symfony 3.2.</p>
  
  <p>I reckon the natural way for doing this would be a GET action form. It would even allow a customer to change the url directly with the good username to see its profile.</p>
  
  <p>Here is the code of my controller : </p>
  
  <p><kbd>ProfileController.php</kbd></p>
  
  <pre class="lang-php prettyprint-override"><code>//...
  
  /** @Route("/profil/search", name="profil_search") */
  public function searchAction() {          
      $builder = $this-&gt;createFormBuilder();
      $builder                
          -&gt;setAction($this-&gt;generateUrl('profil_show'))
          -&gt;setMethod('GET')
          -&gt;add('username', SearchType::class, array('label' =&gt; 'Username : '))
          -&gt;add('submit', SubmitType::class, array('label' =&gt; 'Search'));
      $form = $builder-&gt;getForm();
  
      return $this-&gt;render('profils/profil_search.html.twig', [
          'form' =&gt; $form-&gt;createView(),
      ]);
  }   
  
  /** @Route("/profil/show/{username}", name="profil_show") */
  public function showAction($username) {      
      $repository = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:User');
      $searchedUser = $repository-&gt;findOneByUsername($username);
  
      return $this-&gt;render('profils/profil_show.html.twig', [
          'searchedUser' =&gt; $searchedUser,
      ]);
  }   
  
  //...
  </code></pre>
  
  <p>This code will lead to the following error message : </p>
  
  <blockquote>
    <p>Some mandatory parameters are missing ("username") to generate a URL for
    route "profil_show".</p>
  </blockquote>
  
  <p>I read the <a href="https://symfony.com/doc/current/form/action_method.html" rel="nofollow noreferrer">documentation</a> thoroughly but couldn't guess, how can I pass the <code>username</code> variable to the <code>profil_show</code> route as a parameter ?</p>
  
  <p>If my way of doing is not the good one, thanks for telling me in comments but I'd still like to know how to use GET forms.</p>
  
  <p><strong>EDIT :</strong> </p>
  
  <p>Thanks to @MEmerson answer, I get it now. So for future noobs like me, here is how I did it :</p>
  
  <pre class="lang-php prettyprint-override"><code>/** @Route("/profil/search", name="profil_search") */
  public function searchAction(Request $request) {     
      $data = array();
      $builder = $this-&gt;createFormBuilder($data);
      $builder                
          //-&gt;setAction($this-&gt;generateUrl('profil_show'))
          //-&gt;setMethod('GET')
          -&gt;add('username', SearchType::class, array('label' =&gt; 'Username : '))
          -&gt;add('submit', SubmitType::class, array('label' =&gt; 'Search'));
      $form = $builder-&gt;getForm();
  
      $form-&gt;handleRequest($request);
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
          $data = $form-&gt;getData();
          return $this-&gt;redirectToRoute('profil_show', array('username' =&gt; $data["username"]));
      }
  
      return $this-&gt;render('profils/profil_search.html.twig', [
          'method' =&gt; __METHOD__,
          'form' =&gt; $form-&gt;createView(),
          'message' =&gt; $message,
      ]);
  }  
  </code></pre>