1Mojolicious::Routes(3)User Contributed Perl DocumentationMojolicious::Routes(3)
2
3
4
6 Mojolicious::Routes - Always find your destination with routes
7
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
22 Mojolicious::Routes is the core of the Mojolicious web framework.
23
24 See Mojolicious::Guides::Routing for more.
25
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
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 namespaces
59 my $namespaces = $r->namespaces;
60 $r = $r->namespaces(['MyApp::Controller', 'MyApp']);
61
62 Namespaces to load controllers from.
63
64 # Add another namespace to load controllers from
65 push @{$r->namespaces}, 'MyApp::MyController';
66
67 shortcuts
68 my $shortcuts = $r->shortcuts;
69 $r = $r->shortcuts({foo => sub {...}});
70
71 Contains all available shortcuts.
72
73 types
74 my $types = $r->types;
75 $r = $r->types({lower => qr/[a-z]+/});
76
77 Registered placeholder types, by default only "num" is already defined.
78
80 Mojolicious::Routes inherits all methods from
81 Mojolicious::Routes::Route and implements the following new ones.
82
83 add_condition
84 $r = $r->add_condition(foo => sub ($route, $c, $captures, $arg) {...});
85
86 Register a condition.
87
88 $r->add_condition(foo => sub ($route, $c, $captures, $arg) {
89 ...
90 return 1;
91 });
92
93 add_shortcut
94 $r = $r->add_shortcut(foo => sub ($route, @args) {...});
95
96 Register a shortcut.
97
98 $r->add_shortcut(foo => sub ($route, @args) {...});
99
100 add_type
101 $r = $r->add_type(foo => qr/\w+/);
102 $r = $r->add_type(foo => ['bar', 'baz']);
103
104 Register a placeholder type.
105
106 $r->add_type(lower => qr/[a-z]+/);
107
108 continue
109 $r->continue(Mojolicious::Controller->new);
110
111 Continue dispatch chain and emit the hook "around_action" in
112 Mojolicious for every action.
113
114 dispatch
115 my $bool = $r->dispatch(Mojolicious::Controller->new);
116
117 Match routes with "match" and dispatch with "continue".
118
119 lookup
120 my $route = $r->lookup('foo');
121
122 Find route by name with "find" in Mojolicious::Routes::Route and cache
123 all results for future lookups.
124
125 match
126 $r->match(Mojolicious::Controller->new);
127
128 Match routes with Mojolicious::Routes::Match.
129
131 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
132
133
134
135perl v5.34.0 2021-07-22 Mojolicious::Routes(3)