title: Symfony2 dynamic routing to one controller action with multiple optional parameters
body: <p>I am still quite new to Symfony2 and having trouble getting my head around the following problem.</p>
  
  <p>I have a main browse Action in a Controller with the following routes defined (in the controller):</p>
  
  <pre><code>/**
   * @Route("/browse")
   * @Route("/browse/{page}")
   * @Route("/browse/c/{category}/{categoryName}")
   * @Route("/browse/c/{category}/{categoryName}/{page}")
   * @Route("/browse/c/{category}/b/{brand}/{page}")
   * @Route("/browse/b/{brand}")
   * @Route("/browse/b/{brand}/{page}")
   * @Template()
   */
  public function browseAction($category = 0, $page = 1, $brand = 0) {
  </code></pre>
  
  <p>The above routing works with no problems.</p>
  
  <p>The problem is generating urls from twig views or view helpers.</p>
  
  <p>I would have liked to be able to do the following in a view helper:</p>
  
  <pre><code>{{ url('browse', {'brand': '123'}) }}
  </code></pre>
  
  <p>This works ok with the following in routing.yml:</p>
  
  <pre><code>browse:
    pattern: /browse/b/{brand}
    defaults: { _controller: MyCoreBundle:Browse:browse } 
  </code></pre>
  
  <p>I then tried:</p>
  
  <pre><code>browse:
    pattern: /browse/b/{brand}
    pattern: /browse/c/{category}/b/{brand}
    defaults: { _controller: MyCoreBundle:Browse:browse } 
  </code></pre>
  
  <p>But only the last pattern seems to apply and trying to use the following would throw an error:</p>
  
  <pre><code>{{ url('browse', {'brand': '123'}) }}
  </code></pre>
  
  <p>I realise I can create multiple individual routes in routing.yml that are uniquely named. But that means depending on the variables that will be used I need to specify a different route name which would get very messy quickly.</p>
  
  <p>I also tried:</p>
  
  <pre><code>browse:
    pattern: /browse/c/{category}/b/{brand}/{page}
    defaults: { _controller: MyCoreBundle:Browse:browse } 
  </code></pre>
  
  <p>With:</p>
  
  <pre><code>{{ url('browse', {'brand': '123', 'category':'', 'page': '1'}) }}
  </code></pre>
  
  <p>But that threw an error saying category was not in the correct format..</p>
  
  <p>Am I missing something here? Can somebody point me in the right direction? Do I maybe need to create a twig extension that can take all variables and construct the URL based on input?</p>