Skip to content

Commit

Permalink
added recipe for custom Mojolicious::Lite keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 20, 2012
1 parent c24e99b commit 8087b2b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1082,6 +1082,36 @@ namespace.

1;

=head2 Adding keywords to Mojolicious::Lite

Since L<Mojolicious::Lite> keywords have to be added at compile time, they
require their own exporter module instead of a plugin.

package Mojolicious::Lite::GetPost;
use Mojo::Base 'Exporter';

our @EXPORT = ('get_post');

# Access the router through the application
sub get_post { caller->app->routes->any([qw(GET POST)] => @_) }

1;

Just access the application through the caller's package and you have its full
functionality at your disposal.

use Mojolicious::Lite;
use Mojolicious::Lite::GetPost;

# Accept only GET and POST requests
get_post '/whatever' => sub {
my $self = shift;
my $method = $self->req->method;
$self->render(text => "You called /whatever with $method.");
};

app->start;

=head2 Running code against your application

Ever thought about running a quick oneliner against your L<Mojolicious>
Expand Down

0 comments on commit 8087b2b

Please sign in to comment.