简体   繁体   English

Perl Dancer2 默认路由失败

[英]Perl Dancer2 default route failing

I am wanting to not have perl dancers default 404 "Sorry, this is the void."我不想让 perl dancers 默认 404“抱歉,这里是空的。” response come up when ever a matching route cannot be found.当找不到匹配的路由时,就会出现响应。 I also need to do some other task such as error logging when ever this happens.当发生这种情况时,我还需要执行一些其他任务,例如错误记录。

Here is dancers documentation i followed on creating a default route.这是我在创建默认路线时遵循的舞者文档。 https://metacpan.org/pod/Dancer2::Cookbook#Default-Route https://metacpan.org/pod/Dancer2::Cookbook#Default-Route

This is what i have at the bottom of my main routes file这是我在主要路线文件底部的内容

any qr{.*} => sub {
    status 404;
    template 'test_error_template', { path => request->path };
};

The problem is i still keep getting the default dancer 404 message if an invalid route is requested.问题是如果请求无效路由,我仍然会收到默认的 dancer 404 消息。 Somehow this route is not being picked up.不知何故,这条路线没有被拾起。

This is what comes up in development.log if i try going to a non existing route如果我尝试去一条不存在的路线,这就是 development.log 中出现的内容

[server:5931] core @2020-01-22 10:31:55> looking for get /non_existing_route in /usr/share/perl5/vendor_perl/Dancer2/Core/App.pm l. 36
[server:5931] core @2020-01-22 10:31:55> Entering hook core.error.init in (eval 230) l. 1
[server:5931] core @2020-01-22 10:31:55> Entering hook core.error.before in (eval 230) l. 1
[server:5931] core @2020-01-22 10:31:55> Entering hook core.error.after in (eval 230) l. 1

Can anyone help?谁能帮忙? I do have more than one routes files, could this be part of the issue?我确实有多个路由文件,这可能是问题的一部分吗?

Thanks谢谢

So in my case the problem was having a prefix set in the routes file.所以在我的例子中,问题是在路由文件中设置了前缀。 The default route wouldn't get triggered unless i included the prefix in the url.除非我在 url 中包含前缀,否则不会触发默认路由。 Eg /myprefix/invalid_route would call the default route but just calling /invalid_route wouldn't, resulting in the 404 error.例如/myprefix/invalid_route将调用默认路由,但仅调用/invalid_route不会,从而导致 404 错误。 I have not completely come up with a work around as yet but this does at least answer my original question.我还没有完全想出解决办法,但这至少回答了我原来的问题。

Edit:编辑:

Solved it.解决了。 I created a new route file default.pm that contains only the default route with prefix '/' and put it last in app.psgi.我创建了一个新的路由文件 default.pm,它只包含带有前缀 '/' 的默认路由,并将其放在 app.psgi 的最后。 This way it is reached only when all else fails.这样只有在所有其他方法都失败时才能到达。

app.psgi应用程序.psgi

#!/usr/bin/env perl
use strict;
use warnings;

use main_routes;
use default;
myapp->to_app;

default.pm默认.pm

package default;

prefix '/';

any qr{.*} => sub {
    #log and do other stuff here
    return 'my default route';
};

true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM