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