【问题标题】:How do I match all routes in Mojolicious?如何匹配 Mojolicious 中的所有路线?
【发布时间】:2021-02-08 23:47:24
【问题描述】:

我正在尝试使用 Mojolicious 单线器向用户显示有关服务中断的消息。无论路线如何,我都希望显示消息。这是我从what's in the documentation 略微修改的内容。

perl -Mojo -E 'a("/" => {text => "The service is down for maintenance."})->start' daemon

这适用于/,但不适用于其他任何东西。我添加了一个星号将其转换为通配符路由。

perl -Mojo -E 'a("/*" => {text => "The service is down for maintenance."})->start' daemon

匹配除/ 之外的所有路由。有没有办法匹配单个定义中的所有路由?

【问题讨论】:

标签: perl mojolicious


【解决方案1】:

是的,你可以。试试这些例子:

perl -Mojo -E 'app->routes->get( "/" => { text => "start" }); app->routes->get( "/*any" => { text => "any" }); app->start' get /

perl -Mojo -E 'app->routes->get( "/" => { text => "start" }); app->routes->get( "/*any" => { text => "any" }); app->start' get /some_route

在这里,您在特定路径 / 之后定义捕获所有路径 *any
Documentation

【讨论】:

    【解决方案2】:

    怎么样:

    perl -Mojo -E 'a("/*any" => {text => "The service is down for maintenance."})->start' daemon
    

    我认为它适用于除“/”以外的所有网址。

    【讨论】:

      【解决方案3】:

      如果您创建一个命名占位符,默认值为任何值,我相信它会满足您的需求:

      perl -Mojo -E 'a("/*x" => { text => "The service is down for maintenance.", x => ''})->start' daemon
      

      可能不是你见过的最漂亮的代码,但它只是多了几个字符:-)

      【讨论】:

        猜你喜欢
        • 2018-10-07
        • 2011-03-04
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多