<commit_before># Copyright (C) 2008-2010, <NAME>.

=encoding utf8

=head1 NAME

Mojolicious::Guides::Routing - Routing

=head1 OVERVIEW

This document contains a simple and fun introduction to the L<Mojolicious>
router and it's 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 => '<NAME>'}

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 => '<NAME>'}

=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 => '<NAME>'}

=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 => '<NAME>'}

=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.

    # /bye -> {controller => 'foo-bar', action => 'bye'} -> MyApp::Foo::Bar->bye
    $r->route('/bye')->to('foo-bar#bye');

=head2 Route To Class (C<namespace>, C<class> and C<method>)

From time to time you might want to dispatch to a controller class following
a different naming scheme, for this case there are a few extra stash values
available.

    # /bye -> MyApp::Controller::Foo->bye
    $r->route('/bye')
      ->to(namespace => 'MyApp::Controller', class => 'Foo', method => 'bye');

The C<class> value is entirely optional, so you can just dispatch to a
specific class using C<namespace>.

    # /bye -> MyApp::Controller::Foo->bye
    $r->route('/bye')
      ->to(namespace => 'MyApp::Controller::Foo', method => 'bye');

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<commit_msg>simplified routing guide<commit_after># Copyright (C) 2008-2010, <NAME>.

=encoding utf8

=head1 NAME

Mojolicious::Guides::Routing - Routing

=head1 OVERVIEW

This document contains a simple and fun introduction to the L<Mojolicious>
router and it's 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 => '<NAME>'}

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 => '<NAME>'}

=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 => '<NAME>'}

=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 => '<NAME>'}

=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.

    # /bye -> {controller => 'foo-bar', action => 'bye'} -> MyApp::Foo::Bar->bye
    $r->route('/bye')->to('foo-bar#bye');

=head2 Route To Class (C<namespace> and C<method>)

From time to time you might want to dispatch to a controller class following
a different naming scheme, for this case there are a few extra stash values
available.

    # /bye -> MyApp::Controller::Foo->bye
    $r->route('/bye')
      ->to(namespace => 'MyApp::Controller::Foo', method => 'bye');

=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
{'fraction_non_alphanumeric': 0.1282852903772785, 'fraction_numerical': 0.010173802458668928, 'mean_word_length': 3.2708757637474544, 'pattern_counts': {'":': 0, '<': 88, '<?xml version=': 0, '>': 412, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1192352', 'n_tokens_mistral': 5776, 'n_tokens_neox': 5505, 'n_words': 2149}