Skip to content

Commit

Permalink
explain the arguments of the route generation methods and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 15, 2016
1 parent 8414486 commit 70c177d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

7.02 2016-08-13
7.02 2016-08-15

7.01 2016-08-01
- Improved support for systemd.
Expand Down
48 changes: 29 additions & 19 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -99,10 +99,12 @@ automatically exported.
my $route = any '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = any ['GET', 'POST'] => '/:foo' => sub {...};
my $route = any ['GET', 'POST'] => '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = any
['GET', 'POST'] => '/:foo' => (agent => qr/Firefox/) => sub {...};
Generate route with L<Mojolicious::Routes::Route/"any">, matching any of the
listed HTTP request methods or all. See also L<Mojolicious::Guides::Tutorial>
and L<Mojolicious::Guides::Routing> for many more argument variations.
listed HTTP request methods or all. See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
=head2 app
Expand All @@ -121,21 +123,23 @@ L<Mojolicious>.
my $route = del '/:foo' => sub {...} => 'name';
my $route = del '/:foo' => {foo => 'bar'} => sub {...};
my $route = del '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = del '/:foo' => (agent => qr/Firefox/) => sub {...};
Generate route with L<Mojolicious::Routes::Route/"delete">, matching only
C<DELETE> requests. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
C<DELETE> requests. See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
=head2 get
my $route = get '/:foo' => sub {...};
my $route = get '/:foo' => sub {...} => 'name';
my $route = get '/:foo' => {foo => 'bar'} => sub {...};
my $route = get '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = get '/:foo' => (agent => qr/Firefox/) => sub {...};
Generate route with L<Mojolicious::Routes::Route/"get">, matching only C<GET>
requests. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
requests. See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
=head2 group
Expand All @@ -161,21 +165,23 @@ Share code with L<Mojolicious/"hook">.
my $route = options '/:foo' => sub {...} => 'name';
my $route = options '/:foo' => {foo => 'bar'} => sub {...};
my $route = options '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = options '/:foo' => (agent => qr/Firefox/) => sub {...};
Generate route with L<Mojolicious::Routes::Route/"options">, matching only
C<OPTIONS> requests. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
C<OPTIONS> requests. See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
=head2 patch
my $route = patch '/:foo' => sub {...};
my $route = patch '/:foo' => sub {...} => 'name';
my $route = patch '/:foo' => {foo => 'bar'} => sub {...};
my $route = patch '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = patch '/:foo' => (agent => qr/Firefox/) => sub {...};
Generate route with L<Mojolicious::Routes::Route/"patch">, matching only
C<PATCH> requests. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
C<PATCH> requests. See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
=head2 plugin
Expand All @@ -189,45 +195,49 @@ Load a plugin with L<Mojolicious/"plugin">.
my $route = post '/:foo' => sub {...} => 'name';
my $route = post '/:foo' => {foo => 'bar'} => sub {...};
my $route = post '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = post '/:foo' => (agent => qr/Firefox/) => sub {...};
Generate route with L<Mojolicious::Routes::Route/"post">, matching only C<POST>
requests. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
requests. See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
=head2 put
my $route = put '/:foo' => sub {...};
my $route = put '/:foo' => sub {...} => 'name';
my $route = put '/:foo' => {foo => 'bar'} => sub {...};
my $route = put '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = put '/:foo' => (agent => qr/Firefox/) => sub {...};
Generate route with L<Mojolicious::Routes::Route/"put">, matching only C<PUT>
requests. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
requests. See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
=head2 under
my $route = under sub {...};
my $route = under '/:foo' => sub {...};
my $route = under '/:foo' => {foo => 'bar'};
my $route = under '/:foo' => [foo => qr/\w+/];
my $route = under '/:foo' => (agent => qr/Firefox/);
my $route = under [format => 0];
Generate nested route with L<Mojolicious::Routes::Route/"under">, to which all
following routes are automatically appended. See also
L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for many
more argument variations.
following routes are automatically appended. See
L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more
information.
=head2 websocket
my $route = websocket '/:foo' => sub {...};
my $route = websocket '/:foo' => sub {...} => 'name';
my $route = websocket '/:foo' => {foo => 'bar'} => sub {...};
my $route = websocket '/:foo' => [foo => qr/\w+/] => sub {...};
my $route = websocket '/:foo' => (agent => qr/Firefox/) => sub {...};
Generate route with L<Mojolicious::Routes::Route/"websocket">, matching only
WebSocket handshakes. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
WebSocket handshakes. See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
=head1 ATTRIBUTES
Expand Down
118 changes: 94 additions & 24 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -296,30 +296,73 @@ parent if necessary.
=head2 any
my $route = $r->any;
my $route = $r->any('/:foo');
my $route = $r->any('/:foo' => sub {...});
my $route = $r->any('/:foo' => sub {...} => 'name');
my $route = $r->any('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->any('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->any('/:foo' => (agent => qr/Firefox/) => sub {...});
my $route = $r->any(['GET', 'POST'] => '/:foo' => sub {...});
my $route = $r->any(['GET', 'POST'] => '/:foo' => [foo => qr/\w+/]);
Generate L<Mojolicious::Routes::Route> object matching any of the listed HTTP
request methods or all. See also L<Mojolicious::Guides::Tutorial>
and L<Mojolicious::Guides::Routing> for many more argument variations.
request methods or all.
# Route with destination
# Route with pattern and destination
$r->any('/user')->to('user#whatever');
All arguments are optional, but some have to appear in a certain order, like the
two supported array reference values, which contain the HTTP methods to match
and restrictive placeholders.
# Route with HTTP methods, pattern, restrictive placeholders and destination
$r->any(['DELETE', 'PUT'] => '/:foo' => [foo => qr/\w+/])->to('foo#bar');
There are also two supported string values, containing the route pattern and the
route name, defaulting to the pattern C</> and a name based on the pattern.
# Route with pattern, name and destination
$r->any('/:foo' => 'foo_route')->to('foo#bar');
An arbitrary number of key/value pairs in between the route pattern and name can
be used to specify route conditions.
# Route with pattern, conditions and destination
$r->any('/' => (agent => qr/Firefox/))->to('foo#bar');
A hash reference is used to specifiy optional placeholders and default values
for the stash.
# Route with pattern, optional placeholder and destination
$r->any('/:foo' => {foo => 'bar'})->to('foo#bar');
And a code reference can be used to specify a C<cb> value to be merged into the
default values for the stash.
# Route with pattern and a closure as destination
$r->any('/:foo' => sub {
my $c = shift;
$c->render(text => 'Hello World!');
});
See L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for
more information.

This comment has been minimized.

Copy link
@jhthorsen

jhthorsen Aug 15, 2016

Member

This whole new section is fantastic! 👍

This comment has been minimized.

Copy link
@s1037989

s1037989 Aug 16, 2016

Contributor

Agreed! This makes it really easy to see all the route arguments easily and at once!

=head2 delete
my $route = $r->delete;
my $route = $r->delete('/:foo');
my $route = $r->delete('/:foo' => sub {...});
my $route = $r->delete('/:foo' => sub {...} => 'name');
my $route = $r->delete('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->delete('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->delete('/:foo' => (agent => qr/Firefox/) => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<DELETE> requests.
See also L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing>
for many more argument variations.
Generate L<Mojolicious::Routes::Route> object matching only C<DELETE> requests,
takes the same arguments as L</"any"> (except for the HTTP methods to match,
which are implied). See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
# Route with destination
$r->delete('/user')->to('user#remove');
Expand All @@ -346,14 +389,18 @@ generated ones.
=head2 get
my $route = $r->get;
my $route = $r->get('/:foo');
my $route = $r->get('/:foo' => sub {...});
my $route = $r->get('/:foo' => sub {...} => 'name');
my $route = $r->get('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->get('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->get('/:foo' => (agent => qr/Firefox/) => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<GET> requests.
See also L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing>
for many more argument variations.
Generate L<Mojolicious::Routes::Route> object matching only C<GET> requests,
takes the same arguments as L</"any"> (except for the HTTP methods to match,
which are implied). See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
# Route with destination
$r->get('/user')->to('user#show');
Expand Down Expand Up @@ -397,14 +444,18 @@ the current route.
=head2 options
my $route = $r->options;
my $route = $r->options('/:foo');
my $route = $r->options('/:foo' => sub {...});
my $route = $r->options('/:foo' => sub {...} => 'name');
my $route = $r->options('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->options('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->options('/:foo' => (agent => qr/Firefox/) => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<OPTIONS>
requests. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
requests, takes the same arguments as L</"any"> (except for the HTTP methods to
match, which are implied). See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
# Route with destination
$r->options('/user')->to('user#overview');
Expand Down Expand Up @@ -432,42 +483,54 @@ Parse pattern.
=head2 patch
my $route = $r->patch;
my $route = $r->patch('/:foo');
my $route = $r->patch('/:foo' => sub {...});
my $route = $r->patch('/:foo' => sub {...} => 'name');
my $route = $r->patch('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->patch('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->patch('/:foo' => (agent => qr/Firefox/) => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<PATCH> requests.
See also L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing>
for many more argument variations.
Generate L<Mojolicious::Routes::Route> object matching only C<PATCH> requests,
takes the same arguments as L</"any"> (except for the HTTP methods to match,
which are implied). See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
# Route with destination
$r->patch('/user')->to('user#update');
=head2 post
my $route = $r->post;
my $route = $r->post('/:foo');
my $route = $r->post('/:foo' => sub {...});
my $route = $r->post('/:foo' => sub {...} => 'name');
my $route = $r->post('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->post('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->post('/:foo' => (agent => qr/Firefox/) => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<POST> requests.
See also L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing>
for many more argument variations.
Generate L<Mojolicious::Routes::Route> object matching only C<POST> requests,
takes the same arguments as L</"any"> (except for the HTTP methods to match,
which are implied). See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
# Route with destination
$r->post('/user')->to('user#create');
=head2 put
my $route = $r->put;
my $route = $r->put('/:foo');
my $route = $r->put('/:foo' => sub {...});
my $route = $r->put('/:foo' => sub {...} => 'name');
my $route = $r->put('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->put('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->put('/:foo' => (agent => qr/Firefox/) => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only C<PUT> requests.
See also L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing>
for many more argument variations.
Generate L<Mojolicious::Routes::Route> object matching only C<PUT> requests,
takes the same arguments as L</"any"> (except for the HTTP methods to match,
which are implied). See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
# Route with destination
$r->put('/user')->to('user#replace');
Expand Down Expand Up @@ -542,11 +605,14 @@ Stringify the whole route.
my $route = $r->under('/:foo' => sub {...});
my $route = $r->under('/:foo' => {foo => 'bar'});
my $route = $r->under('/:foo' => [foo => qr/\w+/]);
my $route = $r->under('/:foo' => (agent => qr/Firefox/));
my $route = $r->under([format => 0]);
Generate L<Mojolicious::Routes::Route> object for a nested route with its own
intermediate destination. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
intermediate destination, takes the same arguments as L</"any"> (except for the
HTTP methods to match, which are not available). See
L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more
information.
# Intermediate destination and prefix shared between two routes
my $auth = $r->under('/user')->to('user#auth');
Expand All @@ -568,14 +634,18 @@ restrictions.
=head2 websocket
my $route = $r->websocket;
my $route = $r->websocket('/:foo');
my $route = $r->websocket('/:foo' => sub {...});
my $route = $r->websocket('/:foo' => sub {...} => 'name');
my $route = $r->websocket('/:foo' => {foo => 'bar'} => sub {...});
my $route = $r->websocket('/:foo' => [foo => qr/\w+/] => sub {...});
my $route = $r->websocket('/:foo' => (agent => qr/Firefox/) => sub {...});
Generate L<Mojolicious::Routes::Route> object matching only WebSocket
handshakes. See also L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for many more argument variations.
handshakes, takes the same arguments as L</"any"> (except for the HTTP methods
to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
L<Mojolicious::Guides::Routing> for more information.
# Route with destination
$r->websocket('/echo')->to('example#echo');
Expand Down

0 comments on commit 70c177d

Please sign in to comment.