perl - 在 Mojolicious 中找不到 Controller

标签 perl mojolicious

我在路由中遇到了一些奇怪的结果。

引用PDNController.pm中的代码

    my $r = $self->routes;

    my $auth = $r->under('/' => sub {
    my $self = shift ;
    $self->redirect_to('/login') and return undef unless ($self->is_user_authenticated);
    return 1;
    });

    $r->get('/login')->to('login#index');

    $r->post('/login')->to('login#auth');

    $r->get('/logout')->to('login#logout');

    $auth->get('/')->to('index#index');

    $auth->get('/vlan')->to('vlan#index');

    $auth->get('/api/vlan/add')->to('vlan#add');

引用PDNController/Controller/VLAN.pm中的代码

package PDNController::Controller::VLAN;
use Mojo::Base 'Mojolicious::Controller';

sub index {
  my $self = shift;
  $self->render();
}

sub add {
    my $self = shift;
    my %h;
    $h{error} = '';
    $self->res->headers->add( 'Access-Control-Allow-Origin' => '*' );
    $self->render(json => {%h}});  
}


1;

此示例中除/api/vlan/add 之外的所有路由都工作正常,但最后我有一个错误

[Tue Mar  1 16:54:02 2016] [debug] GET "/api/vlan/add"
[Tue Mar  1 16:54:02 2016] [debug] Routing to a callback
[Tue Mar  1 16:54:02 2016] [debug] Controller "PDNController::Vlan" does not exist
[Tue Mar  1 16:54:02 2016] [debug] Template "vlan/add.html.ep" not found
[Tue Mar  1 16:54:02 2016] [debug] Template "not_found.development.html.ep" not found
[Tue Mar  1 16:54:02 2016] [debug] Template "not_found.html.ep" not found
[Tue Mar  1 16:54:02 2016] [debug] Rendering template "mojo/debug.html.ep"
[Tue Mar  1 16:54:02 2016] [debug] Rendering template "mojo/menubar.html.ep"
[Tue Mar  1 16:54:02 2016] [debug] 404 Not Found (0.052532s, 19.036/s)

为什么 Controller 名称为 PDNController::Vlan 而不是 PDNController::Controller::Vlan ?

Mojolicious 6.51

最佳答案

您的 Controller 名称是PDNController::Controller::VLAN,但您的路线显示:

$auth->get('/vlan')->to('vlan#index');

这意味着您正在搜索显然不存在的 Controller PDNController::Controller::Vlan( Controller 名称被 Mojolicious 更改为 initcap)。

有两种解决方案:

  1. 将 Controller 名称 PDNController::Controller::VLAN 更改为 PDNController::Controller::Vlan 并将 Controller 目录中的文件更改为 Vlan.pm
  2. 或者将路线更改为以下。

    $auth->get('/vlan')->to('VLAN#index');

Why controller name PDNController::Vlan and not PDNController::Controller::Vlan

错误消息中的

PDNController::Vlan指向 Controller PDNController::Controller::Vlan。错误消息旨在让您更加清楚。

当它说 Controller "PDNController::Vlan" 时,意味着它是 PDNController 应用程序的 Controller Vlan(应用程序的所有 Controller 都位于 Controller 目录中),即它说的是 PDNController:: Controller ::Vlan

关于perl - 在 Mojolicious 中找不到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35716888/

相关文章:

sql - 我可以在 DBIx::Class 中漂亮地打印 DBIC_TRACE 输出吗?

perl - 我的 ETrade OAuth 获取 token 请求有什么问题?

perl - 有关提供静态文件的文档

authentication - Mojolicious 基本登录

perl - 如何在 Mojolicious::Lite 应用程序中使用我自己的子程序(全局)

perl - 我可以在 Perl 中重命名另一个散列中的散列键吗?

css - 在文件中提取特定的 css 类

bash - 将文本粘贴到终端

perl - 如何在独立的 Perl 脚本中使用 Mojolicious 渲染?

html - Mojolicious 响应的基本 URL