1

I'm building a Q&A site like StackOverflow with Zend Framework. It's all set except fancy urls. Urls should like this:

wwww.site.com/programming/questions/321345/question-title-sanitized-with-dashes

or

www.site.com/photography/questions/45621/photography-site-question

But also i need site homepages like:

www.site.com/programming

I think i have to use route chaining but some help will be nice.

2
  • what are your current routes?
    – Femaref
    Jul 2, 2011 at 19:31
  • I didn't changed the default /:module/:controller/:action style routing. because i couldn't figure out where to begin.
    – cnkt
    Jul 2, 2011 at 19:33

1 Answer 1

3

You can achieve this quite easily by using custom routes. Since you didn't specify anything about how you have structured your code, I'm going to make the following assumptions:

  • Your site has "sections" like photography and programming which are found based on a parameter
  • You have a single controller which handles questions for all sections, but chooses everything based on the parameters

First, for the homepage case:

Route :section, and specify defaults for module, controller and action which handles your section homepages.

Second, for the question case:

Route :section/questions/:id/:title, and again specify defaults for module, controller and action which handles specific questions. You could also define the title's default as empty, because the question can already be found just based on the ID.

Then, in your controllers you can simply use something like $this->_getParam('section') to determine the section that should be used, and so on.

If you don't know how to create these routes, I suggest you peruse the manual for Zend_Controller_Router and manual for Zend_Application resources for how to define routes in your application.ini

2
  • I accepted your answer because your thinking is true. But i dont want to re-introduce every route when just one thing changes. let me clear this. i have created the site route like this ":section", it's ok. The route matches. In your thinking, for questions, the route should ":section/questions/:id/:title" that's ok too. But we are re-introducing the "section route". My problem is: can we chain all this routes? Because if i want to add a "answers" page, i have to ":section/questions/:id/:title/answers". But creating "/answers" route and chaining it to "questions" route sounds more effective.
    – cnkt
    Jul 2, 2011 at 20:59
  • Yes, you can chain routes but depending on a few things it may or may not work as you'd like. See docs for chaining routes Jul 2, 2011 at 21:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.