Mojolic...::Route(3)

๐ŸŒธ NAME ๐ŸŒธ

Mojolicious::Routes::Route - Route. This is the Mojolicious::Routes::Route(3) page from User Contributed Perl Documentation, bestie. ๐Ÿ’…

โœจ SYNOPSIS โœจ

use Mojolicious::Routes::Route;

my $r = Mojolicious::Routes::Route->new;

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

Mojolicious::Routes::Route is literally the route container used by Mojolicious::Routes. No cap. ๐Ÿซถ

๐Ÿ’– ATTRIBUTES ๐Ÿ’–

Mojolicious::Routes::Route implements the following attributes, and yes, every single one gets its moment. โœจ

children ๐Ÿงธ

my $children = $r->children;
$r           = $r->children([Mojolicious::Routes::Route->new]);

The children of this route โ€” aka the nested routes โ€” used for nesting routes. Cute little route family. ๐Ÿ’•

inline ๐ŸŽ€

my $bool = $r->inline;
$r       = $r->inline($bool);

Allow "under" semantics for this route. Lowkey iconic.

parent ๐ŸŒˆ

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 โ€” so don't expect it to hold on forever, bestie. ๐Ÿ’€

partial ๐Ÿ“

my $bool = $r->partial;
$r       = $r->partial($bool);

Route has no specific end, remaining characters will be captured in "path". It's giving open-ended main character energy. โœจ

pattern ๐Ÿฆ‹

my $pattern = $r->pattern;
$r          = $r->pattern(Mojolicious::Routes::Pattern->new);

Pattern for this route, defaults to a Mojolicious::Routes::Pattern object. Obsessed with a sensible default. ๐Ÿ’…

๐Ÿ”ฅ METHODS ๐Ÿ”ฅ

Mojolicious::Routes::Route inherits all methods from Mojo::Base and implements the following new ones. Let's go, bestie. ๐Ÿ’–

add_child ๐Ÿงš

$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. Route custody battle handled automatically, periodt.

# Reattach route
$r->add_child($r->find('foo'));

any ๐Ÿ‘‘

my $route = $r->any;
my $route = $r->any('/:foo');
my $route = $r->any('/:foo' => sub ($c) {...});
my $route = $r->any('/:foo' => sub ($c) {...} => 'name');
my $route = $r->any('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->any('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->any('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
my $route = $r->any(['GET', 'POST'] => '/:foo' => sub ($c) {...});
my $route = $r->any(['GET', 'POST'] => '/:foo' => [foo => qr/\w+/]);

Generate a Mojolicious::Routes::Route object matching any of the listed HTTP request methods or all. It's flexible like that. ๐Ÿ’…

# 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. Order matters, bestie โ€” don't play yourself. ๐Ÿ˜ญ

# 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 ($c) {
  $c->render(text => 'Hello World!');
});

See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. ๐Ÿซถ

delete ๐Ÿ’€

my $route = $r->delete;
my $route = $r->delete('/:foo');
my $route = $r->delete('/:foo' => sub ($c) {...});
my $route = $r->delete('/:foo' => sub ($c) {...} => 'name');
my $route = $r->delete('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->delete('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->delete('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});

Generate a Mojolicious::Routes::Route object matching only "DELETE" requests. It 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');

find ๐Ÿ”

my $route = $r->find('foo');

Find child route by name; custom names have precedence over automatically generated ones. Custom name supremacy, fr fr.

# Change default parameters of a named route
$r->find('show_user')->to(foo => 'bar');

get ๐ŸŒŸ

my $route = $r->get;
my $route = $r->get('/:foo');
my $route = $r->get('/:foo' => sub ($c) {...});
my $route = $r->get('/:foo' => sub ($c) {...} => 'name');
my $route = $r->get('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->get('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->get('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});

Generate a Mojolicious::Routes::Route object matching only "GET" requests. It 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');

has_custom_name ๐Ÿท๏ธ

my $bool = $r->has_custom_name;

Check if this route has a custom name. Does it have main character name energy? ๐Ÿ’…

has_websocket ๐Ÿ”Œ

my $bool = $r->has_websocket;

Check if this route has a WebSocket ancestor and cache the result for future checks. Efficient and observant.

is_endpoint ๐ŸŽฏ

my $bool = $r->is_endpoint;

Check if this route qualifies as an endpoint. Is it that girl? Yes.

is_reserved ๐Ÿ”’

my $bool = $r->is_reserved('controller');

Check if string is a reserved stash value. Keep your hands off the reserved ones, bestie.

is_websocket ๐Ÿ•ธ๏ธ

my $bool = $r->is_websocket;

Check if this route is a WebSocket.

methods ๐Ÿ“ฎ

my $methods = $r->methods;
$r          = $r->methods('GET');
$r          = $r->methods('GET', 'POST');
$r          = $r->methods(['GET', 'POST']);

Restrict HTTP methods this route is allowed to handle; defaults to no restrictions. Boundaries? We love them.

# Route with two methods and destination
$r->any('/foo')->methods('GET', 'POST')->to('foo#bar');

name ๐Ÿ’–

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. Don't steal "current"'s identity, bestie. ๐Ÿ™ˆ

# Route with destination and custom name
$r->get('/user')->to('user#show')->name('show_user');

options ๐ŸŽ›๏ธ

my $route = $r->options;
my $route = $r->options('/:foo');
my $route = $r->options('/:foo' => sub ($c) {...});
my $route = $r->options('/:foo' => sub ($c) {...} => 'name');
my $route = $r->options('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->options('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->options('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});

Generate a Mojolicious::Routes::Route object matching only "OPTIONS" requests. It 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');

parse ๐Ÿงฉ

$r = $r->parse('/user/:id');
$r = $r->parse('/user/:id', id => qr/\d+/);
$r = $r->parse(format => ['json', 'yaml']);

Parse pattern. Simple, iconic, done. โœจ

patch ๐Ÿฉน

my $route = $r->patch;
my $route = $r->patch('/:foo');
my $route = $r->patch('/:foo' => sub ($c) {...});
my $route = $r->patch('/:foo' => sub ($c) {...} => 'name');
my $route = $r->patch('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->patch('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->patch('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});

Generate a Mojolicious::Routes::Route object matching only "PATCH" requests. It 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');

post ๐Ÿ“

my $route = $r->post;
my $route = $r->post('/:foo');
my $route = $r->post('/:foo' => sub ($c) {...});
my $route = $r->post('/:foo' => sub ($c) {...} => 'name');
my $route = $r->post('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->post('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->post('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});

Generate a Mojolicious::Routes::Route object matching only "POST" requests. It 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');

put ๐Ÿ”

my $route = $r->put;
my $route = $r->put('/:foo');
my $route = $r->put('/:foo' => sub ($c) {...});
my $route = $r->put('/:foo' => sub ($c) {...} => 'name');
my $route = $r->put('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->put('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->put('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});

Generate a Mojolicious::Routes::Route object matching only "PUT" requests. It 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');

query ๐Ÿ”Ž

my $route = $r->query;
my $route = $r->query('/:foo');
my $route = $r->query('/:foo' => sub ($c) {...});
my $route = $r->query('/:foo' => sub ($c) {...} => 'name');
my $route = $r->query('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->query('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->query('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});

Generate a Mojolicious::Routes::Route object matching only "QUERY" requests. It 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->query('/user')->to('user#search');

remove ๐Ÿงน

$r = $r->remove;

Remove route from parent. Clean break, no drama. ๐Ÿ’…

# Remove route completely
$r->find('foo')->remove;

# Reattach route to new parent
$r->any('/foo')->add_child($r->find('bar')->remove);

render ๐ŸŽฌ

my $path = $r->render({foo => 'bar'});

Render route with parameters into a path. Slay. โœจ

๐ŸŒฑ root

my $root = $r->root;

Your routeโ€™s origin story: this is the Mojolicious::Routes object that this route is a descendant of. ๐Ÿ’–

๐Ÿซถ requires

my $requires = $r->requires;
$r           = $r->requires(foo => 1);
$r           = $r->requires(foo => 1, bar => {baz => 'yada'});
$r           = $r->requires([foo => 1, bar => {baz => 'yada'}]);

Turns on conditions for this route. Heads up: this automatically disables the routing cache, because conditions are too complex for caching โ€” no cap. ๐Ÿซ 

# Route with condition and destination
$r->get('/foo')->requires(host => qr/mojolicious\.org/)->to('foo#bar');

๐ŸŒŸ suggested_method

my $method = $r->suggested_method;

The suggested HTTP method for reaching this route โ€” GET and POST are preferred. โœจ

๐ŸŽ€ to

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'});

Sets default parameters for this route. Thatโ€™s the whole assignment, bestie. ๐Ÿ’…

๐Ÿฆ‹ to_string

my $str = $r->to_string;

Stringifies the whole route โ€” turns the entire thing into a string, periodt. ๐ŸŽ€

๐ŸŒˆ under

my $route = $r->under(sub ($c) {...});
my $route = $r->under('/:foo' => sub ($c) {...});
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 => ['json', 'yaml']]);

Generates a Mojolicious::Routes::Route object for a nested route with its own intermediate destination. It takes the same arguments as any, except for the HTTP methods to match โ€” those are not available here. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. ๐Ÿงš

# Longer version
$r->any('/:foo' => sub ($c) {...})->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');

๐Ÿ‘‘ websocket

my $route = $r->websocket;
my $route = $r->websocket('/:foo');
my $route = $r->websocket('/:foo' => sub ($c) {...});
my $route = $r->websocket('/:foo' => sub ($c) {...} => 'name');
my $route = $r->websocket('/:foo' => {foo => 'bar'} => sub ($c) {...});
my $route = $r->websocket('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
my $route = $r->websocket('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});

Generates a Mojolicious::Routes::Route object matching only WebSocket handshakes. It takes the same arguments as any, except for the HTTP methods to match โ€” those are implied. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. ๐Ÿ”ฅ

# Route with destination
$r->websocket('/echo')->to('example#echo');

๐Ÿ”ฅ SHORTCUTS ๐Ÿ”ฅ

In addition to the ATTRIBUTES and METHODS above, you can also call shortcuts provided by root on Mojolicious::Routes::Route objects. Slay. ๐Ÿ’…

# Add a "firefox" shortcut
$r->root->add_shortcut(firefox => sub ($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');

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

Go peek at Mojolicious, Mojolicious::Guides, and <https://mojolicious.org> โ€” thatโ€™s the whole SEE ALSO squad, bestie. ๐Ÿซถ


okay thatโ€™s the entire Mojolic...::Route(3) page, and the footer is giving perl v5.42.0 stamped with 2026-07-15 โ€” iconic, mwah ๐Ÿ’‹

๐Ÿ“ฆ where this page comes from