Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added hook recipe to routing guide
  • Loading branch information
kraih committed Feb 29, 2012
1 parent 502ede4 commit c44c2ba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
6 changes: 2 additions & 4 deletions lib/Mojolicious/Guides/Growing.pod
Expand Up @@ -502,14 +502,12 @@ actual action code needs to be changed.

sub startup {
my $self = shift;
my $r = $self->routes;

$self->secret('Mojolicious rocks');
my $users = MyUsers->new;
$self->helper(users => sub { return $users });

# Router
my $r = $self->routes;

$r->any('/' => sub {
my $self = shift;

Expand Down Expand Up @@ -612,12 +610,12 @@ information.

sub startup {
my $self = shift;
my $r = $self->routes;

$self->secret('Mojolicious rocks');
my $users = MyUsers->new;
$self->helper(users => sub { return $users });

my $r = $self->routes;
$r->any('/')->to('login#index')->name('index');
$r->get('/protected')->to('login#protected')->name('protected');
$r->get('/logout')->to('login#logout')->name('logout');
Expand Down
41 changes: 34 additions & 7 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -257,9 +257,7 @@ sensitive.

sub startup {
my $self = shift;

# Router
my $r = $self->routes;
my $r = $self->routes;

# /bye -> {controller => 'foo', action => 'bye'} -> MyApp::Foo->bye
$r->route('/bye')->to(controller => 'foo', action => 'bye');
Expand Down Expand Up @@ -474,7 +472,8 @@ pass.

=head2 Bridges

Bridges unlike nested routes always match and result in additional dispatch
Bridges can be used to share code with multiple nested routes, because unlike
normal nested routes, they always match and result in additional dispatch
cycles.

# /foo -> undef
Expand Down Expand Up @@ -513,6 +512,36 @@ have children.

All children will be ignored if a waypoint matches.

=head2 Hooks

Hooks operate outside the routing system and allow you to extend
L<Mojolicious> itself by sharing code with all requests indiscriminately,
which makes them a very powerful tool especially for plugins.

# Application
package MyApp;
use Mojo::Base 'Mojolicious';

sub startup {
my $self = shift;
my $r = $self->routes;

# Check all requests for a "/test" prefix
$self->hook(before_dispatch => sub {
my $self = shift;
$self->render(text => 'This request did not reach the router.')
if $self->req->url->path->contains('/test');
});

# Routes that will not be reached if the hook above renders a response
$r->route('/welcome')->to('foo#welcome');
$r->route('/bye')->to('foo#bye');
}

1;

For a full list of available hooks see L<Mojolicious/"hook">.

=head2 Mojolicious::Lite routes

L<Mojolicious::Lite> routes are in fact just a small convenience layer around
Expand Down Expand Up @@ -687,13 +716,11 @@ applications.

sub startup {
my $self = shift;
my $r = $self->routes;

# Plugin
$self->plugin('WerewolfCondition');

# Routes
my $r = $self->routes;

# /hideout (keep them out for 4 days after full moon)
$r->route('/hideout')->over(werewolf => 4)
->to(controller => 'foo', action => 'bar');
Expand Down

0 comments on commit c44c2ba

Please sign in to comment.