Skip to content

Commit

Permalink
more consistent hook examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 18, 2013
1 parent 30863d7 commit 1b3439a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 62 deletions.
12 changes: 6 additions & 6 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -246,9 +246,9 @@ you can use a C<before_dispatch> hook to rewrite incoming requests.

# Change scheme if "X-Forwarded-Protocol" header is set to "https"
app->hook(before_dispatch => sub {
my $self = shift;
$self->req->url->base->scheme('https')
if $self->req->headers->header('X-Forwarded-Protocol') eq 'https';
my $c = shift;
$c->req->url->base->scheme('https')
if $c->req->headers->header('X-Forwarded-Protocol') eq 'https';
});

Since reverse proxies generally don't pass along information about path
Expand All @@ -257,9 +257,9 @@ incoming requests is also quite common.

# Move first part and slash from path to base path in production mode
app->hook(before_dispatch => sub {
my $self = shift;
push @{$self->req->url->base->path->trailing_slash(1)},
shift @{$self->req->url->path->leading_slash(0)};
my $c = shift;
push @{$c->req->url->base->path->trailing_slash(1)},
shift @{$c->req->url->path->leading_slash(0)};
}) if app->mode eq 'production';

L<Mojo::URL> objects are very easy to manipulate, just make sure that the URL
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -812,17 +812,17 @@ C<after_render>.
use IO::Compress::Gzip 'gzip';

hook after_render => sub {
my ($self, $output, $format) = @_;
my ($c, $output, $format) = @_;

# Check if "gzip => 1" has been set in the stash
return unless $self->stash->{gzip};
return unless $c->stash->{gzip};

# Check if user agent accepts GZip compression
return unless ($self->req->headers->accept_encoding // '') =~ /gzip/i;
$self->res->headers->append(Vary => 'Accept-Encoding');
return unless ($c->req->headers->accept_encoding // '') =~ /gzip/i;
$c->res->headers->append(Vary => 'Accept-Encoding');

# Compress content with GZip
$self->res->headers->content_encoding('gzip');
$c->res->headers->content_encoding('gzip');
gzip $output, \my $compressed;
$$output = $compressed;
};
Expand Down
16 changes: 8 additions & 8 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -626,9 +626,9 @@ which makes them a very powerful tool especially for plugins.

# 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');
my $c = shift;
$c->render(text => 'This request did not reach the router.')
if $c->req->url->path->contains('/test');
});

# These will not be reached if the hook above renders a response
Expand All @@ -644,17 +644,17 @@ common use.

# Make sure static files are cached
$self->hook(after_static => sub {
my $self = shift;
$self->res->headers->cache_control('max-age=3600, must-revalidate');
my $c = shift;
$c->res->headers->cache_control('max-age=3600, must-revalidate');
});

Same for monitoring tasks.

# Forward exceptions to a web service
$self->hook(after_dispatch => sub {
my $self = shift;
return unless my $e = $self->stash('exception');
$self->ua->post('https://example.com/bugs' => form => {exception => $e});
my $c = shift;
return unless my $e = $c->stash('exception');
$c->ua->post('https://example.com/bugs' => form => {exception => $e});
});

You can even extend much of the core functionality.
Expand Down
52 changes: 25 additions & 27 deletions t/mojolicious/dispatcher_lite_app.t
Expand Up @@ -16,66 +16,64 @@ use Test::Mojo;

# Internal redirect
hook around_dispatch => sub {
my ($next, $self) = @_;
my ($next, $c) = @_;
$next->();
if ($self->res->code == 404) {
$self->req->url->path($self->param('wrap') ? '/wrap/again' : '/');
delete $self->stash->{$_} for keys %{$self->stash};
$self->tx->res(Mojo::Message::Response->new);
if ($c->res->code == 404) {
$c->req->url->path($c->param('wrap') ? '/wrap/again' : '/');
delete $c->stash->{$_} for keys %{$c->stash};
$c->tx->res(Mojo::Message::Response->new);
$next->();
}
};

# Wrap whole application
hook around_dispatch => sub {
my ($next, $self) = @_;
return $self->render(text => 'Wrapped again!')
if $self->req->url->path->contains('/wrap/again');
my ($next, $c) = @_;
return $c->render(text => 'Wrapped again!')
if $c->req->url->path->contains('/wrap/again');
$next->();
};

# Wrap whole application again
hook around_dispatch => sub {
my ($next, $self) = @_;
return $self->render(text => 'Wrapped!')
if $self->req->url->path->contains('/wrap');
my ($next, $c) = @_;
return $c->render(text => 'Wrapped!')
if $c->req->url->path->contains('/wrap');
$next->();
};

# Custom dispatcher /hello.txt
hook before_dispatch => sub {
my $self = shift;
$self->render(text => 'Custom static file works!')
if $self->req->url->path->contains('/hello.txt');
my $c = shift;
$c->render(text => 'Custom static file works!')
if $c->req->url->path->contains('/hello.txt');
};

# Custom dispatcher /custom
hook before_dispatch => sub {
my $self = shift;
$self->render(text => $self->param('a'), status => 205)
if $self->req->url->path->contains('/custom');
my $c = shift;
$c->render(text => $c->param('a'), status => 205)
if $c->req->url->path->contains('/custom');
};

# Custom dispatcher /custom_too
hook before_routes => sub {
my $self = shift;
$self->render(text => 'this works too')
if $self->req->url->path->contains('/custom_too');
my $c = shift;
$c->render(text => 'this works too')
if $c->req->url->path->contains('/custom_too');
};

# Cleared response for /res.txt
hook before_routes => sub {
my $self = shift;
return
unless $self->req->url->path->contains('/res.txt')
&& $self->param('route');
$self->tx->res(Mojo::Message::Response->new);
my $c = shift;
return unless $c->req->url->path->contains('/res.txt') && $c->param('route');
$c->tx->res(Mojo::Message::Response->new);
};

# Set additional headers for static files
hook after_static => sub {
my $self = shift;
$self->res->headers->cache_control('max-age=3600, must-revalidate');
my $c = shift;
$c->res->headers->cache_control('max-age=3600, must-revalidate');
};

# Make controller available as $_
Expand Down
12 changes: 6 additions & 6 deletions t/mojolicious/exception_lite_app.t
Expand Up @@ -74,18 +74,18 @@ get '/trapped/too' => sub {
# Reuse exception and snapshot
my ($exception, $snapshot);
hook after_dispatch => sub {
my $self = shift;
return unless $self->req->url->path->contains('/reuse/exception');
$exception = $self->stash('exception');
$snapshot = $self->stash('snapshot');
my $c = shift;
return unless $c->req->url->path->contains('/reuse/exception');
$exception = $c->stash('exception');
$snapshot = $c->stash('snapshot');
};

# Custom exception handling
hook around_dispatch => sub {
my ($next, $self) = @_;
my ($next, $c) = @_;
unless (eval { $next->(); 1 }) {
die $@ unless $@ eq "CUSTOM\n";
$self->render(text => 'Custom handling works!');
$c->render(text => 'Custom handling works!');
}
};

Expand Down
6 changes: 3 additions & 3 deletions t/mojolicious/group_lite_app.t
Expand Up @@ -69,9 +69,9 @@ get '/bridge2stash' =>

# Make sure after_dispatch can make session changes
hook after_dispatch => sub {
my $self = shift;
return unless $self->req->url->path->contains('/late/session');
$self->session(late => 'works!');
my $c = shift;
return unless $c->req->url->path->contains('/late/session');
$c->session(late => 'works!');
};

get '/late/session' => sub {
Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/layouted_lite_app.t
Expand Up @@ -20,8 +20,8 @@ app->renderer->paths->[0] = app->home->rel_dir('does_not_exist');

# Reverse filter
hook after_render => sub {
my ($self, $output, $format) = @_;
return unless $self->stash->{reverse};
my ($c, $output, $format) = @_;
return unless $c->stash->{reverse};
$$output = reverse $$output . $format;
};

Expand Down
10 changes: 5 additions & 5 deletions t/mojolicious/rebased_lite_app.t
Expand Up @@ -13,17 +13,17 @@ use Test::Mojo;
# Rebase hook
app->hook(
before_dispatch => sub {
my $self = shift;
$self->req->url->base(Mojo::URL->new('http://example.com/rebased/'));
$self->req->url->path->leading_slash(0);
my $c = shift;
$c->req->url->base(Mojo::URL->new('http://example.com/rebased/'));
$c->req->url->path->leading_slash(0);
}
);

# Current route hook
app->hook(
after_dispatch => sub {
my $self = shift;
$self->res->headers->header('X-Route' => $self->current_route);
my $c = shift;
$c->res->headers->header('X-Route' => $c->current_route);
}
);

Expand Down

0 comments on commit 1b3439a

Please sign in to comment.