FW/1 模式匹配 N 位数字

FW/1 pattern matching N digits

我正在尝试匹配 ID 恰好有 6 个数字的路线

这不起作用:

variables.framework.routes = [
    { "main/{id:[0-9]{6}}"  = "main/home/eid/:id"},
    { "main/home"                   = "main/home"},
    { "*"                           = "main/404"}
    ];

这样做:

variables.framework.routes = [
    { "main/{id:[0-9]+}"    = "main/home/eid/:id"},
    { "main/home"                   = "main/home"},
    { "*"                           = "main/404"}
    ];

第二个当然匹配任意数量的数字。我想知道我是否必须逃脱 {

看起来 FW/1 只允许对路由声明使用有限的正则表达式语法。所以我认为你的第一个例子不会奏效。据我所知,路由中的 limited 正则表达式语法已添加到 FW/1 3.5 版中。我发现了一些关于该主题的讨论以及描述所请求行为的特定评论 - https://github.com/framework-one/fw1/issues/325#issuecomment-118572702

{placeholder:regex}, so we could have product/{id:[0-9]+}-:name.html that targets product.detail?id={id:[0-9]+}&name=:name.

  • You need to repeat the placeholder with the regex in the target route too (could be changed).
  • You can't put } in your placeholder specific regex.

Let me know if a PR is welcome for this add-on.

请注意,第二个要点提到占位符正则表达式中不允许使用 }(方括号)。

这是 3.5 中包含的 pull-request 引用的代码的 link - https://github.com/framework-one/fw1/commit/9543b78552dbd27a526083ac72a3846bd86eeb90

这是 3.5 版更新文档的 link,其中添加了有关此功能的一些信息 - http://framework-one.github.io/documentation/developing-applications.html#url-routes

该文档的片段在这里:

Placeholder variables in the route are identified either by a leading colon or by braces (specifying a variable name and a regex to restrict matches) and can appear in the URL as well, for example { "/product/:id" = "/product/view/id/:id" } specifies a match for /product/something which will be treated as if the URL was /product/view/id/something - section: product, item: view, query string id=something. Similarly, { "/product/{id:[0-9]+}" = "/product/view/id/:id" } specifies a match for /product/42 which will be treated as if the URL was /product/view/id/42, and only numeric values will match the placeholder.