简体   繁体   English

匹配主干路由中的空路由和斜杠?

[英]Matching an empty route and a trailing slash in Backbone route?

I know that there are some issues with having duplicate content (SEO), but that is not something that my project is concerned with. 我知道拥有重复内容(SEO)会遇到一些问题,但这不是我的项目所关心的。

In my backbone router, I have this : 在我的骨干路由器中,我有这个:

routes: {
    "": "startOrder",
    "order/:orderNumber/:stepName": "getOrder",
    "order/:orderNumber/:stepName/": "getOrder"
},

Notice that the second and third routes lead to the same thing. 请注意,第二条和第三条路线导致相同的结果。

The problem is if someone enters a URL ending with simply the "/" character, I would like it to call a separate function (and then remove the slash), but this doesn't happen, it always matches to the blank route. 问题是,如果有人输入的URL仅以“ /”字符结尾,我希望它调用一个单独的函数(然后删除斜杠),但这不会发生,它总是与空白路由匹配。 Then I get issues later on because the URL has a slash where it shouldn't. 然后,稍后我遇到问题,因为该URL在不应有的地方带有斜线。

Am I missing something? 我想念什么吗?

It is now possible to put a slash within parenthesis as an optional part of the route: 现在可以在括号内放置斜线作为路由的可选部分:

var Router = Backbone.Router.extend({
  routes: {
    'order/:orderNumber/:stepName(/)': 'getOrder'
  },
  // ...
});

From a pretty comprehensive issue thread on the topic of slashes at the end of Backbone routes . 摘自骨干网路线末尾斜线主题的相当全面的话题

But as also noted in that thread, to prevent multiple URLs being recorded for your pages it's best to remove the slash at the end of paths in Apache or other server configuration. 但是,正如该线程中所指出的那样,为防止为您的页面记录多个URL,最好在Apache或其他服务器配置的路径末尾删除斜杠。

it's always matching to the blank route instead of no match because you're probably not starting Backbone.history.start with { pushState: true } , so start it like this Backbone.history.start({pushState: true }); 它总是匹配空白路由,而不是没有对手的,因为你可能无法启动Backbone.history.start{ pushState: true } ,所以像这样启动它Backbone.history.start({pushState: true });

if you want a route that matches anything that ends with a / , you can use a *splat and end it with a / . 如果您想要的路由与以/结束的任何内容匹配,则可以使用*splat并以/结束。 something like this should work: 这样的事情应该工作:

routes: {
    "": "startOrder",
    "order/:orderNumber/:stepName": "getOrder",
    "order/:orderNumber/:stepName/": "getOrder",
    "*dummy/": "endsWithSlash"
}

make sure that the dummy route is last. 确保虚拟路由是最后一条。 otherwise, the dummy route will get matched before your order with a slash. 否则,虚拟路线将在您的订单之前带有斜线的匹配。

This routes is internally converted into regular expressions. 该路由在内部转换为正则表达式。 You can pass raw regular expression through route method with your two cases 您可以在两种情况下通过route方法传递原始正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM