Introduction to the Mojolicious router and its underlying concepts
Translations of this material:
- into Russian: Основы работы с маршрутизатором запросов Mojolicious и его основные принципы . Translation complete.
-
Submitted for translation by Foxcool 08.07.2010
Published 1 year, 10 months ago.
Text
# Copyright (C) 2008-2010, Sebastian Riedel.
=encoding utf8
=head1 NAME
Mojolicious::Guides::Routing - Routing
=head1 OVERVIEW
This document contains a simple and fun introduction to the L<Mojolicious>
router and its underlying concepts.
=head1 CONCEPTS
Essentials every L<Mojolicious> developer should know.
=head2 Dispatcher
The foundation of every web framework is a tiny black box connecting incoming
requests with code generating the appropriate response.
GET /user/show/1 -> $self->render(text => 'Sebastian!');
This black box is usually called a dispatcher.
There are many implementations using different strategies to establish these
connections, but pretty much all are based around mapping the requests path
to some kind of response generator.
/user/show/1 -> $self->render(text => 'Sebastian!');
/user/show/2 -> $self->render(text => 'Sara!');
/user/show/3 -> $self->render(text => 'Baerbel!');
/user/show/4 -> $self->render(text => 'Wolfgang!');
While it is very well possible to make all these connections static, it is
also rather inefficient.
Thats why regular expressions are commonly used to make the dispatch process
more dynamic.
qr|/user/show/(\d+)| -> $self->render(text => $users{$1});
Modern dispatchers have pretty much everything HTTP has to offer at their
disposal and can use many more variables than just the request path, such as
request method and headers like C<Host>, C<User-Agent> and C<Accept>.
GET /user/show/23 HTTP/1.1
Host: mojolicious.org
User-Agent: Mozilla/5.0 (compatible; Mojolicious; Perl)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
=head2 Routes
While regular expressions are quite powerful they also tend to be unpleasant
to look at and are generally overkill for ordinary path matching.
qr|/user/show/(\d+)| -> $self->render(text => $users{$1});
This is where routes come into play, they have been designed from the ground
up to represent paths with placeholders.
/user/show/:id -> $self->render(text => $users{$id});
The only difference between a static path and the route above is the C<:id>
placeholder.
One or more placeholders can be anywhere in the route.
/user/:action/:id
A fundamental concept of the L<Mojolicious> router is that extracted
placeholder values are turned into a hash.
/user/show/23 -> /user/:action/:id -> {action => 'show', id => 23}
This hash is basically the center of every L<Mojolicious> application, you
will learn more about this later on.
Internally routes get compiled to regular expressions, so you can get the
best of both worlds with a little bit of experience.
/user/show/:id -> qr/(?-xism:^\/user\/show/([^\/\.]+))/
=head2 Reversibility
One more huge advantage routes have over regular expressions is that they are
easily reversible, extracted placeholders can be turned back into a path at
any time.
/sebastian -> /:name -> {name => 'sebastian'}
{name => 'sebastian'} -> /:name -> /sebastian
=head2 Generic Placeholders
Generic placeholders are the simplest form of placeholders and match
all characters except C</> and C<.>.
/hello -> /:name/hello -> undef
/sebastian/23/hello -> /:name/hello -> undef
/sebastian.23/hello -> /:name/hello -> undef
/sebastian/hello -> /:name/hello -> {name => 'sebastian'}
/sebastian23/hello -> /:name/hello -> {name => 'sebastian23'}
/sebastian 23/hello -> /:name/hello -> {name => 'sebastian 23'}
A generic placeholder can be surrounded by backets to separate it from the
surrounding text.
/hello -> /(:name)hello -> undef
/sebastian/23hello -> /(:name)hello -> undef
/sebastian.23hello -> /(:name)hello -> undef
/sebastianhello -> /(:name)hello -> {name => 'sebastian'}
/sebastian23hello -> /(:name)hello -> {name => 'sebastian23'}
/sebastian 23hello -> /(:name)hello -> {name => 'sebastian 23'}
=head2 Relaxed Placeholders
Relaxed placeholders are very similar to generic placeholders but always
require brackets and match all characters except C</>.
/hello -> /(.name)/hello -> undef
/sebastian/23/hello -> /(.name)/hello -> undef
/sebastian.23/hello -> /(.name)/hello -> {name => 'sebastian.23'}
/sebastian/hello -> /(.name)/hello -> {name => 'sebastian'}
/sebastian23/hello -> /(.name)/hello -> {name => 'sebastian23'}
/sebastian 23/hello -> /(.name)/hello -> {name => 'sebastian 23'}
=head2 Wildcard Placeholders
Wildcard placeholders are just like relaxed placeholders but match absolutely
everything.
/hello -> /(*name)/hello -> undef
/sebastian/23/hello -> /(*name)/hello -> {name => 'sebastian/23'}
/sebastian.23/hello -> /(*name)/hello -> {name => 'sebastian.23'}
/sebastian/hello -> /(*name)/hello -> {name => 'sebastian'}
/sebastian23/hello -> /(*name)/hello -> {name => 'sebastian23'}
/sebastian 23/hello -> /(*name)/hello -> {name => 'sebastian 23'}
=head1 BASICS
Most commonly used features every L<Mojolicious> developer should know about.
=head2 Minimal Route
Every L<Mojolicious> application has a router object you can use to generate
routes structures.
# Application
package MyApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
# Router
my $r = $self->routes;
# Route
$r->route('/welcome')->to(controller => 'foo', action => 'welcome');
}
1;
The minimal static route above will load and instantiate the class
C<MyApp::Foo> and call its C<welcome> method.
# Controller
package MyApp::Foo;
use base 'Mojolicious::Controller';
# Action
sub welcome {
my $self = shift;
# Render response
$self->render(text => 'Hello there!');
}
1;
Routes are usually configured in the C<startup> method of the application
class, but the router can be accessed from everywhere (even at runtime).
=head2 Routing Destination
After you start a new route with the C<route> method you can also give it a
destination in the form of a hash using the chained C<to> method.
# /welcome -> {controller => 'foo', action => 'welcome'}
$r->route('/welcome')->to(controller => 'foo', action => 'welcome');
Now if the route matches an incoming request it will use the content of this
hash to try and find appropriate code to generate a response.
=head2 Stash
The generated hash of a matching route is actually the center of the whole
L<Mojolicious> request cycle.
We call it the stash, and it is basically a global namespace that persists
until a response has been generated.
# /bye -> {controller => 'foo', action => 'bye', mymessage => 'Bye!'}
$r->route('/bye')
->to(controller => 'foo', action => 'bye', mymessage => 'Bye!');
There are a few stash values with special meaning, such as C<controller> and
C<action>, but you can generally fill it with whatever data you need to
generate a response.
Once dispatched the whole stash content can be changed at any time.
=head2 Special Stash Values (C<controller> and C<action>)
When the dispatcher sees C<controller> and C<action> values in the stash it
will always try to turn them into a class and method to dispatch to.
The C<controller> value gets camelized and prefixed with a C<namespace>
(defaulting to the applications class) while the action value is not changed
at all, because of this both values are case sensitive.
# Application
package MyApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
# Router
my $r = $self->routes;
# /bye -> {controller => 'foo', action => 'bye'} -> MyApp::Foo->bye
$r->route('/bye')->to(controller => 'foo', action => 'bye');
}
1;
# Controller
package MyApp::Foo;
use base 'Mojolicious::Controller';
# Action
sub bye {
my $self = shift;
# Render response
$self->render(text => 'Good bye!');
}
1;
Controller classes are perfect for organizing code in larger projects.
There are more dispatch strategies, but because controllers are the most
commonly used ones they also got a special shortcut in the form of
C<controller#action>.
# /bye -> {controller => 'foo', action => 'bye', mymessage => 'Bye!'}
$r->route('/bye')->to('foo#bye', mymessage => 'Bye!');
During camelization C<-> gets replaced with C<::>, this allows multi level
C<controller> hierarchies.
# / -> {controller => 'foo-bar', action => 'hi'} -> MyApp::Foo::Bar->hi
$r->route('/')->to('foo-bar#hi');
=head2 Route To Class (C<namespace>)
From time to time you might want to dispatch to a whole different C<namespace>.
# /bye -> MyApp::Controller::Foo->bye
$r->route('/bye')
->to(namespace => 'MyApp::Controller::Foo', action => 'bye');
The C<controller> is always appended to the C<namespace> if available.
# /bye -> MyApp::Controller::Foo->bye
$r->route('/bye')->to('foo#bye', namespace => 'MyApp::Controller');
You can also change the default namespace for all routes.
$r->namespace('MyApp::Controller');
=head2 Route To Callback (C<cb>)
You can use the C<cb> stash value to bypass controllers and execute a
callback instead.
$r->route('/bye')->to(cb => sub {
my $self = shift;
$self->render(text => 'Good bye!');
});
This technique is the foundation of L<Mojolicious::Lite>, you can learn more
about it from the included tutorial.
=head2 Formats
File extensions like C<.html> and C<.txt> at the end of a route are
automatically detected and stored in the stash value C<format>.
# /foo -> {controller => 'foo', action => 'bar'}
# /foo.html -> {controller => 'foo', action => 'bar', format => 'html'}
# /foo.txt -> {controller => 'foo', action => 'bar', format => 'txt'}
$r->route('/foo')->to(controller => 'foo', action => 'bar');
This for example allows multiple templates for different formats to share the
same code.
=head2 Placeholders And Destinations
Extracted placeholder values will simply redefine older stash values if they
already exist.
# /bye -> {controller => 'foo', action => 'bar', mymessage => 'bye'}
# /hey -> {controller => 'foo', action => 'bar', mymessage => 'hey'}
$r->route('/:mymessage')
->to(controller => 'foo', action => 'bar', mymessage => 'hi');
One more interesting effect, if a placeholder is at the end of a route and
there is already a stash value of the same name present, it automatically
becomes optional.
# / -> {controller => 'foo', action => 'bar', mymessage => 'hi'}
$r->route('/:mymessage')
->to(controller => 'foo', action => 'bar', mymessage => 'hi');
© 2008-2010, Sebastian Riedel..
