Skip to content

Commit

Permalink
use slightly more advanced example for bridges
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 4, 2014
1 parent d632239 commit f69342b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

5.05 2014-06-04
5.05 2014-06-05

5.04 2014-06-03
- Added expect_close attribute to Mojo::Content.
Expand Down
47 changes: 34 additions & 13 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -535,12 +535,12 @@ routes are only evaluated if the callback returned a true value.
use Mojolicious::Lite;
# Authenticate based on name parameter
under sub {
under {allow => 'Bender'} => sub {
my $self = shift;
# Authenticated
my $name = $self->param('name') || '';
return 1 if $name eq 'Bender';
return 1 if $name eq '$self->stash('allow');
# Not authenticated
$self->render('denied');
Expand All @@ -554,10 +554,10 @@ routes are only evaluated if the callback returned a true value.
__DATA__
@@ denied.html.ep
You are not Bender, permission denied.
You are not <%= $allow %>, permission denied.
@@ index.html.ep
Hi Bender.
Hi <%= $allow %>.
Prefixing multiple routes is another good use for L</"under">.
Expand Down Expand Up @@ -1007,11 +1007,13 @@ automatically exported.
=head2 any
my $route = any '/:foo' => sub {...};
my $route = any '/:foo' => {foo => 'bar'} => sub {...};
my $route = any '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = any [qw(GET POST)] => '/:foo' => sub {...};
Generate route with L<Mojolicious::Routes::Route/"any">, matching any of the
listed HTTP request methods or all. See also the tutorial above for more
listed HTTP request methods or all. See also the tutorial above for many more
argument variations.
=head2 app
Expand All @@ -1022,17 +1024,22 @@ The L<Mojolicious::Lite> application.
=head2 del
my $route = del '/:foo' => sub {...};
my $route = del '/:foo' => {foo => 'bar'} => sub {...};
my $route = del '/:foo' => [foo => qr/\w+/] => sub {...};
Generate route with L<Mojolicious::Routes::Route/"delete">, matching only
C<DELETE> requests. See also the tutorial above for more argument variations.
C<DELETE> requests. See also the tutorial above for many more argument
variations.
=head2 get
my $route = get '/:foo' => sub {...};
my $route = get '/:foo' => {foo => 'bar'} => sub {...};
my $route = get '/:foo' => [foo => qr/\w+/] => sub {...};
Generate route with L<Mojolicious::Routes::Route/"get">, matching only C<GET>
requests. See also the tutorial above for more argument variations.
requests. See also the tutorial above for many more argument variations.
=head2 group
Expand All @@ -1054,18 +1061,23 @@ Share code with L<Mojolicious/"hook">.
=head2 options
my $route = options '/:foo' => sub {...};
my $route = options '/:foo' => {foo => 'bar'} => sub {...};
my $route = options '/:foo' => [foo => qr/\w+/] => sub {...};
Generate route with L<Mojolicious::Routes::Route/"options">, matching only
C<OPTIONS> requests. See also the tutorial above for more argument
C<OPTIONS> requests. See also the tutorial above for many more argument
variations.
=head2 patch
my $route = patch '/:foo' => sub {...};
my $route = patch '/:foo' => {foo => 'bar'} => sub {...};
my $route = patch '/:foo' => [foo => qr/\w+/] => sub {...};
Generate route with L<Mojolicious::Routes::Route/"patch">, matching only
C<PATCH> requests. See also the tutorial above for more argument variations.
C<PATCH> requests. See also the tutorial above for many more argument
variations.
=head2 plugin
Expand All @@ -1075,33 +1087,42 @@ Load a plugin with L<Mojolicious/"plugin">.
=head2 post
my $route = post '/:foo' => sub {...};
my $route = post '/:foo' => {foo => 'bar'} => sub {...};
my $route = post '/:foo' => [foo => qr/\w+/] => sub {...};
Generate route with L<Mojolicious::Routes::Route/"post">, matching only
C<POST> requests. See also the tutorial above for more argument variations.
C<POST> requests. See also the tutorial above for many more argument
variations.
=head2 put
my $route = put '/:foo' => sub {...};
my $route = put '/:foo' => {foo => 'bar'} => sub {...};
my $route = put '/:foo' => [foo => qr/\w+/] => sub {...};
Generate route with L<Mojolicious::Routes::Route/"put">, matching only C<PUT>
requests. See also the tutorial above for more argument variations.
requests. See also the tutorial above for many more argument variations.
=head2 under
my $bridge = under sub {...};
my $bridge = under '/:foo';
my $bridge = under '/:foo' => sub {...};
my $bridge = under '/:foo' => [foo => qr/\w+/];
my $bridge = under {format => 0};
Generate bridge route with L<Mojolicious::Routes::Route/"under">, to which all
following routes are automatically appended. See also the tutorial above for
more argument variations.
=head2 websocket
my $route = websocket '/:foo' => sub {...};
my $route = websocket '/:foo' => {foo => 'bar'} => sub {...};
my $route = websocket '/:foo' => [foo => qr/\w+/] => sub {...};
Generate route with L<Mojolicious::Routes::Route/"websocket">, matching only
WebSocket handshakes. See also the tutorial above for more argument
WebSocket handshakes. See also the tutorial above for many more argument
variations.
=head1 ATTRIBUTES
Expand Down
48 changes: 37 additions & 11 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -316,12 +316,15 @@ current parent if necessary.
=head2 any
my $route = $r->any('/:foo');
my $route = $r->any('/:foo' => sub {...});
my $route = $r->any('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->any('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->any([qw(GET POST)] => '/:foo' => sub {...});
Generate L<Mojolicious::Routes::Route> object matching any of the listed HTTP
request methods or all. See also the L<Mojolicious::Lite> tutorial for more
argument variations.
request methods or all. See also the L<Mojolicious::Lite> tutorial for many
more argument variations.
$r->any('/user')->to('user#whatever');
Expand All @@ -341,10 +344,13 @@ object.
=head2 delete
my $route = $r->delete('/:foo');
my $route = $r->delete('/:foo' => sub {...});
my $route = $r->delete('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->delete('/:foo' => [foo => qr/\w+/] => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<DELETE>
requests. See also the L<Mojolicious::Lite> tutorial for more argument
requests. See also the L<Mojolicious::Lite> tutorial for many more argument
variations.
$r->delete('/user')->to('user#remove');
Expand All @@ -370,10 +376,13 @@ generated ones.
=head2 get
my $route = $r->get('/:foo');
my $route = $r->get('/:foo' => sub {...});
my $route = $r->get('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->get('/:foo' => [foo => qr/\w+/] => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<GET> requests.
See also the L<Mojolicious::Lite> tutorial for more argument variations.
See also the L<Mojolicious::Lite> tutorial for many more argument variations.
$r->get('/user')->to('user#show');
Expand Down Expand Up @@ -428,10 +437,13 @@ if necessary.
=head2 options
my $route = $r->options('/:foo');
my $route = $r->options('/:foo' => sub {...});
my $route = $r->options('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->options('/:foo' => [foo => qr/\w+/] => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<OPTIONS>
requests. See also the L<Mojolicious::Lite> tutorial for more argument
requests. See also the L<Mojolicious::Lite> tutorial for many more argument
variations.
$r->options('/user')->to('user#overview');
Expand All @@ -458,28 +470,37 @@ Parse pattern.
=head2 patch
my $route = $r->patch('/:foo');
my $route = $r->patch('/:foo' => sub {...});
my $route = $r->patch('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->patch('/:foo' => [foo => qr/\w+/] => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<PATCH> requests.
See also the L<Mojolicious::Lite> tutorial for more argument variations.
See also the L<Mojolicious::Lite> tutorial for many more argument variations.
$r->patch('/user')->to('user#update');
=head2 post
my $route = $r->post('/:foo');
my $route = $r->post('/:foo' => sub {...});
my $route = $r->post('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->post('/:foo' => [foo => qr/\w+/] => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<POST> requests.
See also the L<Mojolicious::Lite> tutorial for more argument variations.
See also the L<Mojolicious::Lite> tutorial for many more argument variations.
$r->post('/user')->to('user#create');
=head2 put
my $route = $r->put('/:foo');
my $route = $r->put('/:foo' => sub {...});
my $route = $r->put('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->put('/:foo' => [foo => qr/\w+/] => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<PUT> requests.
See also the L<Mojolicious::Lite> tutorial for more argument variations.
See also the L<Mojolicious::Lite> tutorial for many more argument variations.
$r->put('/user')->to('user#replace');
Expand Down Expand Up @@ -546,10 +567,12 @@ Stringify the whole route.
=head2 under
my $bridge = $r->under(sub {...});
my $bridge = $r->under('/:foo');
my $bridge = $r->under('/:foo' => sub {...});
my $bridge = $r->under('/:foo' => [foo => qr/\w+/]);
my $bridge = $r->under({format => 0});
Generate L<Mojolicious::Routes::Route> object for bridge route. See also the
L<Mojolicious::Lite> tutorial for more argument variations.
L<Mojolicious::Lite> tutorial for many more argument variations.
my $auth = $r->under('/user')->to('user#auth');
$auth->get('/show')->to('#show');
Expand All @@ -569,10 +592,13 @@ restrictions.
=head2 websocket
my $route = $r->websocket('/:foo');
my $route = $r->websocket('/:foo' => sub {...});
my $route = $r->websocket('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->websocket('/:foo' => [foo => qr/\w+/] => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only WebSocket
handshakes. See also the L<Mojolicious::Lite> tutorial for more argument
handshakes. See also the L<Mojolicious::Lite> tutorial for many more argument
variations.
$r->websocket('/echo')->to('example#echo');
Expand Down

0 comments on commit f69342b

Please sign in to comment.