Perl Dancer和使用命名参数和嵌套前缀定义路由
最近,我一直在与Dancer合作创建一个应用程序,但我很难确定如何定义路由Perl Dancer和使用命名参数和嵌套前缀定义路由,perl,dancer,Perl,Dancer,最近,我一直在与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
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]core@0.001133>[hit#1]最后一条匹配路线通过!在里面
/usr/local/share/perl5/Dancer/Route.pm l。216
在舞者日志中,记录传球后的任何动作:
[25781]core@0.001524>[hit#4]正在尝试匹配'GET/11121/info/'
针对中的/^/info$/(由“/info”生成)
/usr/local/share/perl5/Dancer/Route.pm l。84[25781]核心@0.002041>
[hit#4]响应:404in/usr/local/share/perl5/Dancer/Handler.pml。
179
这可能是我错过的一些简单的东西,但到目前为止我还没有任何运气。非常感谢您的帮助
编辑我确实注意到我使用了前缀
不正确,所以我修复了这个问题,我为错误的解释道歉。例如,在外壳中,urllocalhost:3000/12/
的第一部分是数据库记录。所有路由都是基于url字符串的第一部分记录构建的,因此我希望在进一步研究路由之前对其进行验证
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
我能够在之前设置一个钩子,钩子可以抓住它并使用params散列,但是它目前在不匹配的模式上有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
}
};
我尝试了一个转发
和发送错误
,但都生成了一个ISE,Dancer在日志的最后一个条目上报告了这一点:
29661]core@0.001048>[hit#2]在钩入前进入
/usr/local/share/perl5/Dancer/Hook.pm l。58
非常感谢您提供的任何帮助,也欢迎您对我的问题进行编辑,以使我的问题更加清晰。我根本没有与
Dancer
合作过,但从文档来看,您似乎还必须在前缀/info
中定义一条路线。尝试:
# This is the part that is not working
prefix '/info' => sub {
get '/' => sub {
..... # does stuff
}
}; ## back to the root
这不是前缀的作用。Prefix用于声明当前包中路由的前缀
prefix '/users';
get '/' => sub { ... }; # matches /users
post '/add' => sub { ... }; # matches /users/add
get '/view/:id' => sub { ... }; # matches /users/view/123
你尝试访问的哪个URL不起作用?不清楚你想要实现什么。您的URL与您在上一个日志中看到的任何声明的路由都不匹配:
'GET/11121/info/”与/^/info$/
。如果你想使用这个URL,你需要像any'/*/info'
而不是/info
那样声明路由,你的问题到底是什么?文档提到前缀的范围可以像prefix'/foo'=>sub{ROUTES}
。在这种情况下,这也会起作用吗?