Mojolicious 通配符占位符和问号
通配符占位符 (*) 据说可以匹配所有内容。 但我担心它不会...
我有一个具有以下方法的网络服务:
get '/*param' => sub {
my $self = shift;
my $param = $self->stash('param');
$self->app->log->debug($param);
}
当我使用以下方法查询我的服务时: http://localhost:3000/搜索 然后该方法记录“搜索”,这是可以的 但 当我查询我的服务时: http://localhost:3000/search?page=1 然后该方法还记录“搜索”,这在我看来是不行的,
我也尝试替换
get '/*param' => sub {
为
get '/:param' => [param => qr/.*/] => sub {
,但结果是相同的。
有人知道解决这个问题的方法吗? 或者我应该将其归档为错误?
问候, 洛伦佐
更新 对于遇到同样问题的人,我已经解决了这个问题,如下所示:
get '/*path' => sub {
my $self = shift;
my $path = $self->stash('path');
my @params = $self->param;
if (scalar @params > 0) {
$path .= '?';
foreach my $param (@params) {
$path .= $param . '=' . $self->param($param) . '&';
}
$path = substr($path, 0, length($path) - 1);
}
$self->app->log->debug($path);
}
The wildcard placeholder (*) is said to match absolutely everything.
But I'm afraid that it doesn't...
I have a webservice with the following method:
get '/*param' => sub {
my $self = shift;
my $param = $self->stash('param');
$self->app->log->debug($param);
}
When i query my service with: http://localhost:3000/search
then the method logs "search" which is ok
but
when i query my service with: http://localhost:3000/search?page=1
then the method also logs "search" which is not ok IMO
I also tried replacing
get '/*param' => sub {
with
get '/:param' => [param => qr/.*/] => sub {
but the result is the same.
Does anybody know of a way around this?
Or should I file this as a bug?
Regards,
Lorenzo
UPDATE
for people with the same problem, I've worked around this issue like this:
get '/*path' => sub {
my $self = shift;
my $path = $self->stash('path');
my @params = $self->param;
if (scalar @params > 0) {
$path .= '?';
foreach my $param (@params) {
$path .= $param . '=' . $self->param($param) . '&';
}
$path = substr($path, 0, length($path) - 1);
}
$self->app->log->debug($path);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
?page= 它不是网址。
它的参数。
所以这里没有任何错误。
$param 中有“搜索”。
并且 $page=1 在藏匿处。
?page= its not url.
Its param.
So no any bugs here.
you have 'search' in $param.
And $page=1 in stash.
我认为 Korjavin 是对的,这是预期的行为。看起来像“page=1”作为参数,应该位于 $stash->param('page') 中。请参阅 ::Lite 中的 GET-POST-parameters
如果是不起作用,也许将“param”占位符重命名为其他名称有帮助吗?也许这是名字冲突。
I think Korjavin is right, that's expected behavior. Looks like "page=1" as a parameter and should be in $stash->param('page'). See GET-POST-parameters in ::Lite
If it does not work, maybe renaming the "param" placeholder to something else helps? Maybe it's a name-clash.
请求参数不会在存储中。
它们位于
$self->req->params
中,所以
应该允许您看到您想要的信息
The request parameters wouldn't be in the stash.
They're in
$self->req->params
So
Should allow you to see the information you're after