Mojolicious 通配符占位符和问号

发布于 2024-12-18 08:30:38 字数 1165 浏览 0 评论 0原文

通配符占位符 (*) 据说可以匹配所有内容。 但我担心它不会...

我有一个具有以下方法的网络服务:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉城メ夜风 2024-12-25 08:30:38

?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.

早乙女 2024-12-25 08:30:38

我认为 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.

久随 2024-12-25 08:30:38

请求参数不会在存储中。

它们位于

$self->req->params

中,所以

    my $params = $self->req->params->to_hash;
    $self->app->log->debug(Dumper $params);

应该允许您看到您想要的信息

The request parameters wouldn't be in the stash.

They're in

$self->req->params

So

    my $params = $self->req->params->to_hash;
    $self->app->log->debug(Dumper $params);

Should allow you to see the information you're after

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文