开发者

custom nested routes with Kohana 3

I have a Articles model and a Category model, both with a url_slug variable (what I would like to show up in the url when looking for it. here is how I would like to have the URLs appear:

//list all of the a开发者_如何学编程rticles
http://example.com/articles   

//list of all the articles in that category
http://example.com/articles/:category_slug 

//a single article.
http://example.com/articles/:category_slug/:article_slug

How should I set up the articles controller and/or the routes in order to achieve this?


You can use a route like this

Route::set('articles', '/articles(/<category_filter>(/<article_id>))', array(
    'controller' => 'Articles',
    'action' => '(index)'
))
->defaults(array(
    'controller' => 'Articles',
    'action'     => 'index',
));

In your controller, you can access the filters/ids with

$category   = Request::current()->param('category_filter');
$article_id = Request::current()->param('article_id');

And filter your output accordingly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜