Alister West

home is where your code is ...

Mojolicious Default Routing

Some different ways to route code and the templates they default route to:

# Here are some different ways of using the routing - and the templates they default route to:

# Foo::Controller::Events::welcome -> templates/controller/events/welcome.html.ep
$r->route('/welcome')->to('controller-events#welcome');

# Foo::Controller::Events::welcome -> templates/events/welcome.html.ep
$r->route('/welcome')->to('events#welcome', namespace => 'Foo::Controller');

# Foo::AAA::Authenticate::login -> templates/authenticate/login.html.ep
$r->route('/login')->to('authenticate#login', namespace => 'Foo::AAA');

# Foo::Authenticate::login -> templates/authenticate/login.html.ep
$r->route('/login')->to('authenticate#login', template => '/authenticate/login');

Gotcha's!

# Gotcha! $r->get is not equal to $r->route
#  - restrictive placeholders get requires an array ref.
#  - *route* can take an array ref - but the default docs use pass in a hash.
# these routes do the same thing (mostly)
$r->get(   '/:page.html' , [ page => [qw/about terms/] ] )->to('foo#bar');
$r->route( '/:page.html' , [ page => [qw/about terms/] ] )->to('foo#bar');
$r->route( '/:page.html',    page => [qw/about terms/]   )->to('foo#bar');


# Gotcha! Get the routing object in Mojolicous::Lite for testing routes.
my $r = shift->routes; # nope
my $r = app->routes; # yep!
By Alister West