2

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...

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

I think you forgot to pass these two arguments to your IndexAction() in controller.

Try this code

Public function indexAction($page,$ads_on_page)
{}

Hope this helps you.

2 Comments

initialize the variables with default values in arguments of the action,then try it.indexAction($page=1,$ads_on_page=2)
Thank you, I tried it, but it doesn't help. The error is on the routing step. So the system does not reach the controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.