我们如何识别动态URL中的参数? [英] How do we identify parameters in a dynamic URL?

查看:407
本文介绍了我们如何识别动态URL中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个Rails CMS,博客或新闻列表可以出现在站点树中的任何位置。这意味着任何页面都基于数据库字段知道其类型-例如:页面知道其属于新闻列表类型,因此一旦对该页面调用了URL,我们就需要新闻列表控制器对其进行处理。新闻列表页面的子页面是新闻故事-每个新闻页面都有日期和类别。

We are building a Rails CMS where a blog or news listing can appear anywhere in the site tree. This means that any page knows their type based on a database field - eg: a page knows that it is of type newslisting so once that URL is called for that page we need it to be processed by the newslistingcontroller. The subpages of the newslisting page are news stories - each with a date and category.

新闻列表的网址可能是

/dogs/snoopy-news/ 

一个新闻故事可能是

/dogs/snoopy-news/snoopy-is-great/

对于我们来说,复杂性在于URL带有附加参数,用于列出新闻报道或类别列表的日期范围,例如:

The complexity for us is with URLs that have additional parameters in for listing date ranges of news stories or category listings eg:

列出所有11月的新闻报道

Listing all november news stories

/dogs/snoopy-news/2010/11/

或列出所有以食物为分类的故事:

Or listing all stories which have food as their category:

/dogs/snoopy-news/category/food/

既然新闻列表页面可以位于网站层次结构的任何级别,那么我们将如何识别2010是参数还是类别是参数?

Since that newslisting page could be at any level in the site hierarchy how would we approach identifying that 2010 is a parameter or that category is a parameter?

(我们会将所有网址存储在数据库中每个页面,以便我们可以首先查找以查看是否存在用于URL的页面)

(We will store all URLs in the database for each page so we can lookup first to see if a page exists for a URL)

编辑:这篇文章提供了可能的解决方案:动态CMS,例如在红宝石上的路线,我们将对其进行尝试并进行报告。在CMS中保存页面后,我们不仅会在页面上添加路由,还会在页面上添加可能的参数:

This post gives a possible solution: Dynamic CMS like routes in ruby on rails which we will try out and report back on. On save of a page in the CMS we would add routes to the routing table at that point for not only the page itself but for also possible parameters:

/dogs/snoopy-news/{year}/{month}/


推荐答案

您可以使用路由遍历和约束来匹配适当的模式。

You can use route globbing and constraints to match a proper pattern.

# Rails 2.x
map.connect "*path/:year/:month", 
             :constraints => {:year => /\d{4}/, :month => /0[1-9]|1[0-2]/ },
             :controller => :pages, :action => :month_archive

# Rails 3.x
match "*path/:year/:month" => "pages#month_archive", 
             :constraints => {:year => /\d{4}/, :month => /0[1-9]|1[0-2]/ }

这将匹配 / dogs / snoopy-news / 2010/11 并通过:path => dogs / snoopy-news,:year => 2010,:month => params哈希中为 11 。它将匹配所有以一年零一个月作为最后两个细分的所有路线,而不管事先有多少个细分。并且它将拒绝在最后两个细分中与年份和月份不匹配的任何路线。使用:path 参数的操作取决于控制器。

This will match /dogs/snoopy-news/2010/11 and pass :path => "dogs/snoopy-news", :year => "2010", :month => "11" in the params hash. It will match all routes that have a year and a month as the last two segments, regardless of how many segments come beforehand. And it will reject any route that doesn't match a proper year and month in the last two segments. What you do with the :path parameter is up to you in the controller.

这篇关于我们如何识别动态URL中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆