Skip to content

Commit

Permalink
mention shortcuts too
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 18, 2016
1 parent ecd7f7e commit 027208a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
18 changes: 12 additions & 6 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -120,7 +120,7 @@ parentheses.

=head2 Typed placeholders

You can also assign your placeholders a type, such as
You can also assign your placeholders a type to change their behavior, such as
L<Mojolicious::Routes::Pattern/"relaxed">, which allows them to match all
characters except C</>, similar to the regular expression C<([^/]+)>.

Expand All @@ -131,14 +131,14 @@ characters except C</>, similar to the regular expression C<([^/]+)>.
/sebastian23/hello -> /(name:relaxed)/hello -> {name => 'sebastian23'}
/sebastian 23/hello -> /(name:relaxed)/hello -> {name => 'sebastian 23'}

This can be especially useful for manually matching file names with extensions,
rather than using L<format detection|/"Formats">.
This can be very useful for manually matching file names with extensions, rather
than relying on L<format detection|/"Formats">.

/music/song.mp3 -> /music/(filename:relaxed) -> {filename => 'song.mp3'}

The type L<Mojolicious::Routes::Pattern/"wildcard"> is very similar, but it
allows your placeholders to match absolutely everything, including C</> and
C<.>, similar to the regular expression C<(.+)>.
The type L<Mojolicious::Routes::Pattern/"wildcard"> is very similar, but allows
your placeholders to match absolutely everything, including C</> and C<.>,
similar to the regular expression C<(.+)>.

/hello -> /(name:wildcard)/hello -> undef
/sebastian/23/hello -> /(name:wildcard)/hello -> {name => 'sebastian/23'}
Expand All @@ -147,6 +147,12 @@ C<.>, similar to the regular expression C<(.+)>.
/sebastian23/hello -> /(name:wildcard)/hello -> {name => 'sebastian23'}
/sebastian 23/hello -> /(name:wildcard)/hello -> {name => 'sebastian 23'}

Because the C<relaxed> and C<wildcard> pleaceholder types are so common, they
also have a shortcut form.

/hello/♥.mojolicious -> /hello/#name -> {name => '♥.mojolicious'}
/hello/♥/mojolicious -> /hello/*name -> {name => '♥/mojolicious'}

For a full list of placeholder types see
L<Mojolicious::Routes::Pattern/"TYPES">.

Expand Down
11 changes: 7 additions & 4 deletions lib/Mojolicious/Guides/Tutorial.pod
Expand Up @@ -320,27 +320,30 @@ L<Mojolicious::Controller/"param">.

# /testsomething/foo
# /test123something/foo
get '/(:bar)something/foo' => sub {
get '/(bar)something/foo' => sub {
my $c = shift;
my $bar = $c->param('bar');
$c->render(text => "Our :bar placeholder matched $bar");
};

app->start;

To separate them from the surrounding text, you can surround your placeholders
with parentheses, which also makes the colon prefix optional.

=head2 Typed placeholders

To change how much of the request path your placeholder should capture, you can
also assign it a type like L<Mojolicious::Routes::Pattern/"wildcard">, which
matches absolutely everything, including C</> and C<.>, similar to the regular
assign it a type like L<Mojolicious::Routes::Pattern/"wildcard">, which matches
absolutely everything, including C</> and C<.>, similar to the regular
expression C<(.+)>.

use Mojolicious::Lite;

# /hello/test
# /hello/test123
# /hello/test.123/test/123
get '/hello/(:you:wildcard)' => 'groovy';
get '/hello/(you:wildcard)' => 'groovy';

app->start;
__DATA__
Expand Down

0 comments on commit 027208a

Please sign in to comment.