开发者

StackOverflow style routing with Zend Framework

开发者 https://www.devze.com 2023-03-17 08:40 出处:网络
I\'m building a Q&A site like StackOverflow with Zend Framework. It\'s all set except fancy urls.

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/progra开发者_JAVA百科mming

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消