title: Symfony - Form redirect
body: <p>I am building simple car renting app in Symfony for a programming class at my university.</p>
  
  <p>On URL /search I show a form to user. Right now when the form is submitted user his /search again but different template is rendered</p>
  
  <p>This is form code:</p>
  
  <pre><code>class SearchQueryType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('pickupCity', TextType::class)
              -&gt;add('returnCity', TextType::class)
              -&gt;add('pickupDateTime', DateTimeType::class, array(
                  'years' =&gt; range(2016,2017),
                  'error_bubbling' =&gt; true,
              ))
              -&gt;add('returnDateTime', DateTimeType::class, array(
                  'years' =&gt; range(2016,2017),
                  'error_bubbling' =&gt; true,
              ))
  
              -&gt;add('save', SubmitType::class, array(
                  'label' =&gt; 'Find Car',
                  'attr' =&gt; array(
                      'class' =&gt; 'btn-secondary btn-lg'
                  )))
          ;
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\SearchQuery',
          ));
      }
  }
  </code></pre>
  
  <p>And controller for this route:</p>
  
  <pre><code>class SearchController extends Controller
  {
      public function searchAction(Request $request)
      {
          $query = new SearchQuery();
  
          $form = $this-&gt;createForm(SearchQueryType::class, $query);
  
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
              /* Some logic here */
  
              return $this-&gt;render('AppBundle:default:results.html.twig', array(
                  'form' =&gt; $form-&gt;createView()
              ));
          }
  
          return $this-&gt;render('AppBundle:default:search.html.twig', array(
              'form' =&gt; $form-&gt;createView()
          ));
      }
  }
  </code></pre>
  
  <p>However when the form is submitted I want to redirect to URL /results?SUBMITTED-PARMS-HERE, so user can send this link to someone and receive the same results. On /results again I wand to render form, this time filled with search params submitted and below render available car.</p>
  
  <p>I don't know if its the best way to handle this problem, current solution works but link can't be send to someone else.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I change the code to fallowing:</p>
  
  <pre><code> if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
              $data = $form-&gt;getData();
              $session = $this-&gt;get('session');
  
              $session-&gt;set('pickupCity', $data-&gt;getPickupCity());
              $session-&gt;set('returnCity', $data-&gt;getReturnCity());
              $session-&gt;set('pickupDateTime', $data-&gt;getPickupDateTime());
              $session-&gt;set('returnDateTime', $data-&gt;getReturnDateTime());
  
              return $this-&gt;redirectToRoute('results', array(
                  'pickupCity' =&gt; $data-&gt;getPickupCity(),
                  'returnCity' =&gt; $data-&gt;getReturnCity(),
                  'pickupDate' =&gt; $data-&gt;getPickupDateTime()-&gt;format('d-m-Y-H-i'),
                  'returnDate' =&gt; $data-&gt;getReturnDateTime()-&gt;format('d-m-Y-H-i'),
               ), 301);
          }
  </code></pre>
  
  <p>So as a result I receive URL ./results/Paris/Berlin/10-02-2016-10-00/17-02-2016-10-00</p>
  
  <p>And in routing.yml:</p>
  
  <pre><code>results:
      path:     /results/{pickupCity}/{returnCity}/{pickupDate}/{returnDate}
      defaults: { _controller: AppBundle:Search:result}
      methods:  [POST]
  </code></pre>
  
  <p>Because I only want to receive POST request on this route and now when I submit form I get error page saying:</p>
  
  <pre><code>No route found for "GET /results/Paris/Berling/01-07-2016-00-00/01-11-2016-00-00": Method Not Allowed (Allow: POST)
  </code></pre>
  
  <p>Any idea how to fix it?</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Never mind, my reasoning was bad. </p>