Mojolicious::Routes::Route(3pm) | User Contributed Perl Documentation | Mojolicious::Routes::Route(3pm) |
Mojolicious::Routes::Route - Route
use Mojolicious::Routes::Route; my $r = Mojolicious::Routes::Route->new;
Mojolicious::Routes::Route is the route container used by Mojolicious::Routes.
Mojolicious::Routes::Route implements the following attributes.
my $children = $r->children; $r = $r->children([Mojolicious::Routes::Route->new]);
The children of this route, used for nesting routes.
my $bool = $r->inline; $r = $r->inline($bool);
Allow "under" semantics for this route.
my $parent = $r->parent; $r = $r->parent(Mojolicious::Routes::Route->new);
The parent of this route, usually a Mojolicious::Routes::Route object. Note that this attribute is weakened.
my $bool = $r->partial; $r = $r->partial($bool);
Route has no specific end, remaining characters will be captured in "path".
my $pattern = $r->pattern; $r = $r->pattern(Mojolicious::Routes::Pattern->new);
Pattern for this route, defaults to a Mojolicious::Routes::Pattern object.
Mojolicious::Routes::Route inherits all methods from Mojo::Base and implements the following new ones.
$r = $r->add_child(Mojolicious::Routes::Route->new);
Add a child to this route, it will be automatically removed from its current parent if necessary.
# Reattach route $r->add_child($r->find('foo'));
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 Mojolicious::Routes::Route object matching any of the listed HTTP request methods or all.
# 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 "/" 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, condition and destination $r->any('/' => (agent => qr/Firefox/))->to('foo#bar');
A hash reference is used to specify 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 "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 Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
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 Mojolicious::Routes::Route object matching only "DELETE" requests, takes the same arguments as "any" (except for the HTTP methods to match, which are implied). See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
# Route with destination $r->delete('/user')->to('user#remove');
$r = $r->detour(action => 'foo'); $r = $r->detour('controller#action'); $r = $r->detour(Mojolicious->new, foo => 'bar'); $r = $r->detour('MyApp', {foo => 'bar'});
Set default parameters for this route and allow partial matching to simplify application embedding, takes the same arguments as "to".
my $route = $r->find('foo');
Find child route by name, custom names have precedence over automatically generated ones.
# Change default parameters of a named route $r->find('show_user')->to(foo => 'bar');
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 Mojolicious::Routes::Route object matching only "GET" requests, takes the same arguments as "any" (except for the HTTP methods to match, which are implied). See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
# Route with destination $r->get('/user')->to('user#show');
my $bool = $r->has_custom_name;
Check if this route has a custom name.
my $bool = $r->has_websocket;
Check if this route has a WebSocket ancestor and cache the result for future checks.
my $bool = $r->is_endpoint;
Check if this route qualifies as an endpoint.
my $bool = $r->is_websocket;
Check if this route is a WebSocket.
my $name = $r->name; $r = $r->name('foo');
The name of this route, defaults to an automatically generated name based on the route pattern. Note that the name "current" is reserved for referring to the current route.
# Route with destination and custom name $r->get('/user')->to('user#show')->name('show_user');
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 Mojolicious::Routes::Route object matching only "OPTIONS" requests, takes the same arguments as "any" (except for the HTTP methods to match, which are implied). See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
# Route with destination $r->options('/user')->to('user#overview');
my $over = $r->over; $r = $r->over(foo => 1); $r = $r->over(foo => 1, bar => {baz => 'yada'}); $r = $r->over([foo => 1, bar => {baz => 'yada'}]);
Activate conditions for this route. Note that this automatically disables the routing cache, since conditions are too complex for caching.
# Route with condition and destination $r->get('/foo')->over(host => qr/mojolicious\.org/)->to('foo#bar');
$r = $r->parse('/:action'); $r = $r->parse('/:action', action => qr/\w+/); $r = $r->parse(format => 0);
Parse pattern.
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 Mojolicious::Routes::Route object matching only "PATCH" requests, takes the same arguments as "any" (except for the HTTP methods to match, which are implied). See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
# Route with destination $r->patch('/user')->to('user#update');
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 Mojolicious::Routes::Route object matching only "POST" requests, takes the same arguments as "any" (except for the HTTP methods to match, which are implied). See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
# Route with destination $r->post('/user')->to('user#create');
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 Mojolicious::Routes::Route object matching only "PUT" requests, takes the same arguments as "any" (except for the HTTP methods to match, which are implied). See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
# Route with destination $r->put('/user')->to('user#replace');
$r = $r->remove;
Remove route from parent.
# Remove route completely $r->find('foo')->remove; # Reattach route to new parent $r->route('/foo')->add_child($r->find('bar')->remove);
my $path = $r->render({foo => 'bar'});
Render route with parameters into a path.
my $root = $r->root;
The Mojolicious::Routes object this route is a descendant of.
my $route = $r->route; my $route = $r->route('/:action'); my $route = $r->route('/:action', action => qr/\w+/); my $route = $r->route(format => 0);
Low-level generator for routes matching all HTTP request methods, returns a Mojolicious::Routes::Route object.
my $method = $r->suggested_method;
Suggested HTTP method for reaching this route, "GET" and "POST" are preferred.
my $defaults = $r->to; $r = $r->to(action => 'foo'); $r = $r->to({action => 'foo'}); $r = $r->to('controller#action'); $r = $r->to('controller#action', foo => 'bar'); $r = $r->to('controller#action', {foo => 'bar'}); $r = $r->to(Mojolicious->new); $r = $r->to(Mojolicious->new, foo => 'bar'); $r = $r->to(Mojolicious->new, {foo => 'bar'}); $r = $r->to('MyApp'); $r = $r->to('MyApp', foo => 'bar'); $r = $r->to('MyApp', {foo => 'bar'});
Set default parameters for this route.
my $str = $r->to_string;
Stringify the whole route.
my $route = $r->under(sub {...}); 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 Mojolicious::Routes::Route object for a nested route with its own intermediate destination, takes the same arguments as "any" (except for the HTTP methods to match, which are not available). See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
# Longer version $r->any('/:foo' => sub {...})->inline(1); # Intermediate destination and prefix shared between two routes my $auth = $r->under('/user')->to('user#auth'); $auth->get('/show')->to('#show'); $auth->post('/create')->to('#create');
my $methods = $r->via; $r = $r->via('GET'); $r = $r->via('GET', 'POST'); $r = $r->via(['GET', 'POST']);
Restrict HTTP methods this route is allowed to handle, defaults to no restrictions.
# Route with two methods and destination $r->route('/foo')->via('GET', 'POST')->to('foo#bar');
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 Mojolicious::Routes::Route object matching only WebSocket handshakes, takes the same arguments as "any" (except for the HTTP methods to match, which are implied). See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
# Route with destination $r->websocket('/echo')->to('example#echo');
In addition to the "ATTRIBUTES" and "METHODS" above you can also call shortcuts provided by "root" on Mojolicious::Routes::Route objects.
# Add a "firefox" shortcut $r->root->add_shortcut(firefox => sub { my ($r, $path) = @_; $r->get($path, agent => qr/Firefox/); }); # Use "firefox" shortcut to generate routes $r->firefox('/welcome')->to('firefox#welcome'); $r->firefox('/bye')->to('firefox#bye');
Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
2019-02-05 | perl v5.28.1 |