Rails/Merb 中的混合动态路线
我试图找出将简单路由映射到一个网址的最佳方法。应用程序包含 4 个模型:位置、类别、预算、公司。但每个模型都可以像这样映射到单个 url。对于现实世界的例子,你可以查看 http://sortfolio.com/
/chicago <- Location
/3k-5k <- Budget
/bars <- Category
/company-name <- Company
而且它们也可以混合成一些东西像这样:
/chicago/3k-5k <- Location, Budget
/chicago/bars/3k-5k <- Location, Category, Budget
/bars/3k-5k <- Category, Budget
/foo-bar <- Company
所以,基本上有一个特定的参数顺序:
Location/Category/Budget
有 2 条路由来匹配所有 url`s:
match('/').to(:controller => 'search', :action => 'index')
match('/:location(/:category(/:budget)))').to(:controller => 'search', :action => 'index')
但是在控制器中它变得有点棘手,因为我必须按特定顺序检查项目。公司 url 放在最后,因为它们可能会覆盖系统路径。
它工作正常,但我必须对每个请求进行大量查找,因为参数是完全动态的。没什么大不了的,这样的信息可以存储在缓存中,但我只是想知道是否还有其他解决方案或方法?
我脑子里只有一个想法:有 1 个额外的表来存储有关路径及其通向何处的信息。换句话说 - 表非规范化。
+----+----------+------------+
| ID | Url | ObjectType |
+----+----------+------------+
| 1 | chicago | location |
| 2 | bars | category |
| 3 | 3k-5k | budget |
| 4 | foo-bar | company |
+----+----------+------------+
url 解析过程可能只包含在一个自定义查询中。
请指教。
Im trying to figure out the best way to map simple routes into one url. Application contains of 4 models: Location, Category, Budget, Company. But each model could be mapped to single url like this. For real-world example you can check out http://sortfolio.com/
/chicago <- Location
/3k-5k <- Budget
/bars <- Category
/company-name <- Company
And also they can be mixed into something like this:
/chicago/3k-5k <- Location, Budget
/chicago/bars/3k-5k <- Location, Category, Budget
/bars/3k-5k <- Category, Budget
/foo-bar <- Company
So, basically there is a specific order of parameters:
Location/Category/Budget
There are 2 routes to match all of the url`s:
match('/').to(:controller => 'search', :action => 'index')
match('/:location(/:category(/:budget)))').to(:controller => 'search', :action => 'index')
But in controller it gets a little tricky, because i have to check for items in specific order. Company url goes last because they might override the system paths.
Its working fine, but i have to make a lot of lookups per request because parameters are totally dynamic. Not a big deal, such info could be stored in cache, but im just wondering if there are any other solutions or approaches?
I have just one on my mind: having 1 additional table to store information about path and where it leads. In other words - table denormalization.
+----+----------+------------+
| ID | Url | ObjectType |
+----+----------+------------+
| 1 | chicago | location |
| 2 | bars | category |
| 3 | 3k-5k | budget |
| 4 | foo-bar | company |
+----+----------+------------+
And url parsing process might be packed just in one custom query.
Please advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论