1

I'm new to Symfony framework. Currently I have route in mybundle/Resources/config/routing.yml:

fcr_category_head:
  path: head/{slug}/{city}/{page}
  defaults: { _controller: AppBundle:Head:index, slug: "", city: "all", page: 1 }
  requirements: {page: "\d+"}

the problem is that city option is available when user uses filter to narrow results to selected city, if city does not exist, the second parameter should be page.

so route variations can be like this:

head/slug/city //default page 1 if city is not a number, if number then it is page
head/slug/city/10 //e.g page 10
head/slug/10 //no city parameter, because it is number, page 10

is it possible to make these combinations in routing file or the only way to solve this is tho write my own logic in controller?

Thank you.

2
  • 2
    This sounds like a good use case for query parameters rather than attributes. That is far more flexible, what if you wanted to add an additional parameter? Look at most ecom sites, they use query param's in filtering products. Commented Mar 31, 2015 at 8:21
  • @Luke Yes, but I have a request, that city has to be in this format. I think it is mostly for SEO, other filter parameters will be defined as query parameters as you suggested. Commented Mar 31, 2015 at 9:40

1 Answer 1

5

You can define two routes for the same action. One with city and one without.

Like this:

fcr_category_head:
  path: head/{slug}/{page}
  defaults: { _controller: AppBundle:Head:index, slug: "", city: "all", page: 1 }
  requirements: {page: "\d+"}

fcr_category_head_with_city:
  path: head/{slug}/{city}/{page}
  defaults: { _controller: AppBundle:Head:index, slug: "", city: "all", page: 1 }
  requirements: {page: "\d+", city: "\w+"}

First will catch all request like this

head/slug/10 
head/slug

And second will catch

head/slug/city
head/slug/city/10
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'm going to try that.

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.