最近我一直在与 Dancer 合作创建一个应用程序,但我很难弄清楚如何定义路由。
package MyApp;
use Dancer ':syntax';
our $VERSION = '0.1';
# Base for routing of requests
# Match against /:validate
any '/:validate' => sub {
# This assumes we can stop the routing here
# validate the request param in the url string
# against a regex and 'pass' the request to a
# specific route with the 'var' option
var validate => params->{validate};
.....
# Validation works and dancer passes successfully
pass();
};
# This is the part that is not working
prefix '/info' => sub {
..... # does stuff
}; ## back to the root
在通行证的舞者日志中:
[25561] 核心@0.001133> [命中#1]最后匹配路由通过!在 /usr/local/share/perl5/Dancer/Route.pm l。216
在通过后的任何内容的舞者日志中:
[25781] 核心 @0.001524> [命中 #4] 尝试在 /usr/local/share/perl5/Dancer/ 中将 'GET /11121/info/' 与 /^/info$/(从 '/info' 生成)匹配路线.pm l. 84 [25781] 核心 @0.002041> [命中 #4] 响应:/usr/local/share/perl5/Dancer/Handler.pm l 中的 404。179
这可能是我缺少的一些简单的东西,但到目前为止我还没有运气。任何帮助是极大的赞赏。
编辑我确实注意到我使用prefix
不正确,所以我修复了这个问题,我为错误的解释道歉。localhost:3000/12/
简而言之,例如,url 的第一部分是数据库记录。所有路由都建立在该记录上,该记录是 url 字符串的第一部分,因此我想在进一步进入路由之前对其进行验证。
我能够设置一个before
钩子来抓取它并可以使用参数哈希,但它目前在不匹配的模式上出现 500 错误。
hook before => sub {
my $route_handler = shift;
var record => params->{record};
my $record = var 'record';
while ($record !~ m/^ID[\-]\d{3,6}$/) { # Check for valid ID
if ($record =~ m/^\d{3,6}$/) { # Works currently
$record = 'ID-'.$record;
}else {forward "/error"}; # this = 500 ISE error
}
};
我尝试了一个forward
,send_error
但都生成了一个 ISE,Dancer 在日志的最后一个条目上报告了这一点:
29661] 核心 @0.001048> [命中 #2] 在 /usr/local/share/perl5/Dancer/Hook.pm l 中挂钩之前进入。58
非常感谢任何帮助,也欢迎编辑以使我的问题更清楚。