Symfony – Two optional variables in Symfony2 routing

routingsymfony

When I try to use 2 optional variables in Symfony2 routing I have th error: No route found for "GET /"
In routing.yml I have:

AcmeAshavatBundle_homepage:
pattern:  /{page}/{ads_on_page}/
defaults: { _controller: AcmeAshavatBundle:Page:index, page:1, ads_on_page:2 }
requirements:
   _method:  GET|POST

And when i go to http://localhost:8080/AshavatSy/web/app_dev.php/ I have the error. The intresting is that if I run http://localhost:8080/AshavatSy/web/app_dev.php/1 it works well.Also, if I change the path to pattern: /main/{page}/{ads_on_page}/ it works well.

What is the problem?
I'd like to ask, that someone will try to do like this [e.g. pattern: /a/b/ defaults: {… a:1,b:2}, or as he thinks you should do it] in his project, and see is it a common problem…

Best Answer

I managed to have something similar working by defining two routes, pointing to the same controller, using default parameters. In my case, using annotations:

/**
 * @Route("/products/{catId}/{prodId}", defaults={"catId"="", "prodId"=""})
 * @Route("/products/")
 * @Template()
 */
public function indexAction($catId = null, $prodId = null) {
    ...

I think that using default parameters only, Symfony would expect two /.

HTH

Related Topic