From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

NAME

Mojolicious::Plugin::Route - Plugin to loader files of routes.

SYNOPSIS

sub under {
my ($self, $r) = @_;
$r->under('/bar');
}
1;
sub route {
my ($self, $bar) = @_;
$bar->get('/foo'); # GET /bar/foo
}
1;

DESCRIPTION

Mojolicious::Plugin::Route - Allows you to create routes through files that will be automatically loaded.

METHODS

route

sub route {
my ($self, $r) = @_;
# will create route /foo
$r->get('/foo' => sub ($c) {...});
}
1;

Method route only creates routes, but the reference not is saved.

any

sub any {
my ($self, $r) = @_;
$r->any('/baz' => sub ($c) {...});
}
1;
sub route {
# receive reference of parent that is MyApp::Route::Baz
my ($self, $baz) = @_;
# will create route /baz/child
$baz->get('/child' => sub ($c) {...});
}
1;

Reference of the method any will be saved to be used in the files with the same namespace, not to be saved you need return undef.

under

sub under {
my ($self, $r) = @_;
$r->under('/bar' => sub ($c) {...});
}
1;
sub route {
# receive reference of parent that is MyApp::Route::Bar
my ($self, $bar) = @_;
# will create route /bar/child
$bar->get('/child' => sub ($c) {...});
}
1;

Similar to method any, the reference of the method under will be saved to be used in the files with the same namespace, not to be saved you need return undef.

OPTIONS

inverse

# Mojolicious::Lite
plugin Route => {inverse => 1};

Inverse option will pass the parameters with inverse format.

If you define inverse to Example 2 your methods need to receive parameters like this:

sub under {
my ($self, $r, $base) = @_; # parameters inverse
}
sub route {
my ($self, $r, $base, $under_above) = @_; # parameters inverse
}

namespace

# Mojolicious::Lite
plugin Route => {namespace => 'Foo'}; # $moniker::Foo

Namespace to load routes from, defaults to $moniker::Route.

EXAMPLES

Example 1

sub under {
my ($self, $r) = @_;
my $under = $r->under('/admin' => sub {
my $c = shift;
return 1 if $c->req->url->to_abs->userinfo eq 'Admin:Password';
$c->res->headers->www_authenticate('Basic');
$c->render(text => 'Authentication required!', status => 401);
return;
});
}
sub route {
my ($self, $under_above, $r) = @_;
$r->get('/login' => sub {
shift->render(text => 'Login');
});
$under_above->get('/' => sub {
shift->render(text => 'Admin');
});
}
1;

Example 2

sub under {
my ($self, $r) = @_;
$r->under('/base');
}
1;
sub under {
my ($self, $base, $r) = @_;
$base->under('/page');
}
sub route {
my ($self, $under_above, $base, $r) = @_;
# will create route /base/page/foo
$under_above->get('/foo' => sub {
shift->render(text => 'Foo');
});
# will create route /base/bar
$base->get('/bar' => sub {
shift->render(text => 'Bar');
});
# will create route /baz
$r->get('/baz' => sub {
shift->render(text => 'Baz');
});
}
1;

Example 3

sub under {
my ($self, $r) = @_;
$r->under('/base');
}
1;
sub route {
my ($self, $base) = @_;
# will create route /base/foo
$base->get('/foo' => sub {
shift->render(text => 'Foo');
});
}
1;
sub route {
my ($self, $base) = @_;
# will create route /base/baz
$base->get('/baz' => sub {
shift->render(text => 'Baz');
});
}
1;

SEE ALSO

Mojolicious, Mojolicious::Routes::Route, Mojolicious::Guides, http://mojolicio.us.

AUTHOR

Lucas Tiago de Moraes, lucastiagodemoraes@gmail.com.

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Lucas Tiago de Moraes.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.