Skip to content

Commit

Permalink
added experimental routes function to Mojolicious::Lite
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 21, 2011
1 parent 809c1f2 commit 73274fe
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
1.99 2011-09-21 00:00:00
- Deprecated direct hash access to the flash in
Mojolicious::Controller.
- Added EXPERIMENTAL routes function to Mojolicious::Lite.
- Added EXPERIMENTAL build_frame and parse_frame methods to
Mojo::Transaction::WebSocket.
- Added EXPERIMENTAL profile helper.
Expand Down
51 changes: 48 additions & 3 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -47,9 +47,14 @@ sub import {
*{"${caller}::hook"} = sub { $app->hook(@_) };
*{"${caller}::under"} = *{"${caller}::ladder"} =
sub { $routes = $root->under(@_) };
*{"${caller}::plugin"} = sub { $app->plugin(@_) };
*{"${caller}::post"} = sub { $routes->post(@_) };
*{"${caller}::put"} = sub { $routes->put(@_) };
*{"${caller}::plugin"} = sub { $app->plugin(@_) };
*{"${caller}::post"} = sub { $routes->post(@_) };
*{"${caller}::put"} = sub { $routes->put(@_) };
*{"${caller}::routes"} = sub (&) {
my $old = $root;
$_[0]->($root = $routes);
$root = $old;
};
*{"${caller}::websocket"} = sub { $routes->websocket(@_) };

# We are most likely the app in a lite environment
Expand Down Expand Up @@ -557,6 +562,39 @@ Prefixing multiple routes is another good use for C<under>.
app->start;
Route blocks allow multiple C<under> statements to be nested and related
routes to be grouped.
use Mojolicious::Lite;
# Global logic shared by all routes
under sub {
my $self = shift;
return 1 if $self->req->headers->header('X-Bender');
$self->render(text => "You're not Bender!");
return;
};
# Admin section
routes {
# Local logic shared only by routes in this block
under '/admin' => sub {
my $self = shift;
return 1 if $self->req->heaers->header('X-Awesome');
$self->render(text => "You're not awesome enough!");
return;
};
# GET /admin/dashboard
get '/dashboard' => {text => 'Nothing to see here yet!'};
};
# GET /welcome
get '/welcome' => {text => 'Hi Bender!'};
app->start;
=head2 Conditions
Conditions such as C<agent> and C<host> from
Expand Down Expand Up @@ -848,6 +886,13 @@ See also the tutorial above for more argument variations.
Generate route matching only C<PUT> requests.
See also the tutorial above for more argument variations.
=head2 C<routes>
routes {...};
Start a new route block.
Note that this function is EXPERIMENTAL and might change without warning!
=head2 C<under>
my $route = under sub {...};
Expand Down
40 changes: 38 additions & 2 deletions t/mojolicious/lite_app.t
Expand Up @@ -10,7 +10,7 @@ BEGIN {
$ENV{MOJO_MODE} = 'development';
}

use Test::More tests => 914;
use Test::More tests => 925;

# Pollution
123 =~ m/(\d+)/;
Expand Down Expand Up @@ -766,11 +766,34 @@ get '/foo' => {inline => '<%= $message %>!'};
get '/bar' => {inline => 'also <%= $message %>!'};

# Reset
under '/';
under '/' => {foo => 'one'};

# GET /reset
get '/reset' => {text => 'reset works!'};

# Block
routes {

# /block
under '/block' => {bar => 'two'};

# GET /block
get {inline => '<%= $foo %><%= $bar %>!'};

# Nested block
routes {

# /block/nested
under '/nested' => {baz => 'three'};

# GET /block/nested
get {inline => '<%= $baz %><%= $bar %><%= $foo %>!'};

# GET /block/nested/whatever
get '/whatever' => {inline => '<%= $foo %><%= $bar %><%= $baz %>!'};
};
};

# Oh Fry, I love you more than the moon, and the stars,
# and the POETIC IMAGE NUMBER 137 NOT FOUND
my $t = Test::Mojo->new;
Expand Down Expand Up @@ -1846,6 +1869,19 @@ $t->get_ok('/reset')->status_is(200)->content_is('reset works!');
# GET /prefix/reset
$t->get_ok('/prefix/reset')->status_is(404);

# GET /block
$t->get_ok('/block')->status_is(200)->content_is("onetwo!\n");

# GET /block/nested
$t->get_ok('/block/nested')->status_is(200)->content_is("threetwoone!\n");

# GET /block/nested/whatever
$t->get_ok('/block/nested/whatever')->status_is(200)
->content_is("onetwothree!\n");

# GET /block/nested/something
$t->get_ok('/block/nested/something')->status_is(404);

# GET /captures/foo/bar
$t->get_ok('/captures/foo/bar')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down

0 comments on commit 73274fe

Please sign in to comment.