我如何匹配Mojolicious中的所有路线?

时间:2015-08-21 19:43:53

标签: perl mojolicious

我正在尝试使用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

匹配除/以外的所有路线。有没有办法在单个定义中匹配所有路线?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

怎么样:

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

我认为它适用于所有网址但是' /'。

答案 2 :(得分:0)

It seems you can not

  

通配符占位符...类似于正则表达式(。+)

这意味着:一个或多个符号。

但您可以尝试使用$r->pattern->regex

答案 3 :(得分:0)

如果您创建一个名为占位符,默认值为any,我相信它可以满足您的需求:

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

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

相关问题