1Mojolicious::Routes(3)User Contributed Perl DocumentationMojolicious::Routes(3)
2
3
4

NAME

6       Mojolicious::Routes - Always find your destination with routes
7

SYNOPSIS

9         use Mojolicious::Routes;
10
11         # Simple route
12         my $r = Mojolicious::Routes->new;
13         $r->any('/')->to(controller => 'blog', action => 'welcome');
14
15         # More advanced routes
16         my $blog = $r->under('/blog');
17         $blog->get('/list')->to('blog#list');
18         $blog->get('/:id' => [id => qr/\d+/])->to('blog#show', id => 23);
19         $blog->patch(sub ($c) { $c->render(text => 'Go away!', status => 405) });
20

DESCRIPTION

22       Mojolicious::Routes is the core of the Mojolicious web framework.
23
24       See Mojolicious::Guides::Routing for more.
25

TYPES

27       These placeholder types are available by default.
28
29   num
30         $r->get('/article/<id:num>');
31
32       Placeholder value needs to be a non-fractional number, similar to the
33       regular expression "([0-9]+)".
34

ATTRIBUTES

36       Mojolicious::Routes inherits all attributes from
37       Mojolicious::Routes::Route and implements the following new ones.
38
39   base_classes
40         my $classes = $r->base_classes;
41         $r          = $r->base_classes(['MyApp::Controller']);
42
43       Base classes used to identify controllers, defaults to
44       Mojolicious::Controller and Mojolicious.
45
46   cache
47         my $cache = $r->cache;
48         $r        = $r->cache(Mojo::Cache->new);
49
50       Routing cache, defaults to a Mojo::Cache object.
51
52   conditions
53         my $conditions = $r->conditions;
54         $r             = $r->conditions({foo => sub {...}});
55
56       Contains all available conditions.
57
58   hidden
59         my $hidden = $r->hidden;
60         $r         = $r->hidden(['attr', 'has', 'new']);
61
62       Controller attributes and methods that are hidden from router, defaults
63       to "attr", "has", "new" and "tap".
64
65   namespaces
66         my $namespaces = $r->namespaces;
67         $r             = $r->namespaces(['MyApp::Controller', 'MyApp']);
68
69       Namespaces to load controllers from.
70
71         # Add another namespace to load controllers from
72         push @{$r->namespaces}, 'MyApp::MyController';
73
74   shortcuts
75         my $shortcuts = $r->shortcuts;
76         $r            = $r->shortcuts({foo => sub {...}});
77
78       Contains all available shortcuts.
79
80   types
81         my $types = $r->types;
82         $r        = $r->types({lower => qr/[a-z]+/});
83
84       Registered placeholder types, by default only "num" is already defined.
85

METHODS

87       Mojolicious::Routes inherits all methods from
88       Mojolicious::Routes::Route and implements the following new ones.
89
90   add_condition
91         $r = $r->add_condition(foo => sub ($route, $c, $captures, $arg) {...});
92
93       Register a condition.
94
95         $r->add_condition(foo => sub ($route, $c, $captures, $arg) {
96           ...
97           return 1;
98         });
99
100   add_shortcut
101         $r = $r->add_shortcut(foo => sub ($route, @args) {...});
102
103       Register a shortcut.
104
105         $r->add_shortcut(foo => sub ($route, @args) {...});
106
107   add_type
108         $r = $r->add_type(foo => qr/\w+/);
109         $r = $r->add_type(foo => ['bar', 'baz']);
110
111       Register a placeholder type.
112
113         $r->add_type(lower => qr/[a-z]+/);
114
115   continue
116         $r->continue(Mojolicious::Controller->new);
117
118       Continue dispatch chain and emit the hook "around_action" in
119       Mojolicious for every action.
120
121   dispatch
122         my $bool = $r->dispatch(Mojolicious::Controller->new);
123
124       Match routes with "match" and dispatch with "continue".
125
126   hide
127         $r = $r->hide('foo', 'bar');
128
129       Hide controller attributes and methods from router.
130
131   is_hidden
132         my $bool = $r->is_hidden('foo');
133
134       Check if controller attribute or method is hidden from router.
135
136   lookup
137         my $route = $r->lookup('foo');
138
139       Find route by name with "find" in Mojolicious::Routes::Route and cache
140       all results for future lookups.
141
142   match
143         $r->match(Mojolicious::Controller->new);
144
145       Match routes with Mojolicious::Routes::Match.
146

SEE ALSO

148       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
149
150
151
152perl v5.32.1                      2021-02-07            Mojolicious::Routes(3)
Impressum