1Mojolicious::Lite(3) User Contributed Perl Documentation Mojolicious::Lite(3)
2
3
4
6 Mojolicious::Lite - Micro real-time web framework
7
9 # Automatically enables "strict", "warnings", "utf8" and Perl 5.16 features
10 use Mojolicious::Lite;
11
12 # Route with placeholder
13 get '/:foo' => sub {
14 my $c = shift;
15 my $foo = $c->param('foo');
16 $c->render(text => "Hello from $foo.");
17 };
18
19 # Start the Mojolicious command system
20 app->start;
21
23 Mojolicious::Lite is a tiny domain specific language built around
24 Mojolicious, made up of only about a dozen Perl functions.
25
26 On Perl 5.20+ you can also use a "-signatures" flag to enable support
27 for subroutine signatures.
28
29 use Mojolicious::Lite -signatures;
30
31 get '/:foo' => sub ($c) {
32 my $foo = $c->param('foo');
33 $c->render(text => "Hello from $foo.");
34 };
35
36 app->start;
37
38 See Mojolicious::Guides::Tutorial for more!
39
41 While Mojolicious::Guides::Growing will give you a detailed
42 introduction to growing a Mojolicious::Lite prototype into a well-
43 structured Mojolicious application, here we have collected a few
44 snippets that illustrate very well just how similar both of them are.
45
46 Routes
47 The functions "get", "post" and friends all have equivalent methods.
48
49 # Mojolicious::Lite
50 get '/foo' => sub {
51 my $c = shift;
52 $c->render(text => 'Hello World!');
53 };
54
55 # Mojolicious
56 sub startup {
57 my $self = shift;
58
59 my $routes = $self->routes;
60 $routes->get('/foo' => sub {
61 my $c = shift;
62 $c->render(text => 'Hello World!');
63 });
64 }
65
66 Application
67 The application object you can access with the function "app" is the
68 first argument passed to the "startup" method.
69
70 # Mojolicious::Lite
71 app->max_request_size(16777216);
72
73 # Mojolicious
74 sub startup {
75 my $self = shift;
76 $self->max_request_size(16777216);
77 }
78
79 Plugins
80 Instead of the "plugin" function you just use the method "plugin" in
81 Mojolicious.
82
83 # Mojolicious::Lite
84 plugin 'Config';
85
86 # Mojolicious
87 sub startup {
88 my $self = shift;
89 $self->plugin('Config');
90 }
91
92 Helpers
93 Similar to plugins, instead of the "helper" function you just use the
94 method "helper" in Mojolicious.
95
96 # Mojolicious::Lite
97 helper two => sub {
98 my $c = shift;
99 return 1 + 1;
100 };
101
102 # Mojolicious
103 sub startup {
104 my $self = shift;
105 $self->helper(two => sub {
106 my $c = shift;
107 return 1 + 1;
108 });
109 }
110
111 Under
112 Instead of sequential function calls, we can use methods to build a
113 tree with nested routes, that much better illustrates how routes work
114 internally.
115
116 # Mojolicious::Lite
117 under '/foo';
118 get '/bar' => sub {...};
119
120 # Mojolicious
121 sub startup {
122 my $self = shift;
123
124 my $routes = $self->routes;
125 my $foo = $routes->under('/foo');
126 $foo->get('/bar' => sub {...});
127 }
128
130 Mojolicious::Lite implements the following functions, which are
131 automatically exported.
132
133 any
134 my $route = any '/:foo' => sub {...};
135 my $route = any '/:foo' => sub {...} => 'name';
136 my $route = any '/:foo' => {foo => 'bar'} => sub {...};
137 my $route = any '/:foo' => [foo => qr/\w+/] => sub {...};
138 my $route = any ['GET', 'POST'] => '/:foo' => sub {...};
139 my $route = any ['GET', 'POST'] => '/:foo' => [foo => qr/\w+/] => sub {...};
140 my $route = any
141 ['GET', 'POST'] => '/:foo' => (agent => qr/Firefox/) => sub {...};
142
143 Generate route with "any" in Mojolicious::Routes::Route, matching any
144 of the listed HTTP request methods or all. See
145 Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more
146 information.
147
148 app
149 my $app = app;
150
151 Returns the Mojolicious::Lite application object, which is a subclass
152 of Mojolicious.
153
154 # Use all the available attributes and methods
155 app->log->level('error');
156 app->defaults(foo => 'bar');
157
158 del
159 my $route = del '/:foo' => sub {...};
160 my $route = del '/:foo' => sub {...} => 'name';
161 my $route = del '/:foo' => {foo => 'bar'} => sub {...};
162 my $route = del '/:foo' => [foo => qr/\w+/] => sub {...};
163 my $route = del '/:foo' => (agent => qr/Firefox/) => sub {...};
164
165 Generate route with "delete" in Mojolicious::Routes::Route, matching
166 only "DELETE" requests. See Mojolicious::Guides::Tutorial and
167 Mojolicious::Guides::Routing for more information.
168
169 get
170 my $route = get '/:foo' => sub {...};
171 my $route = get '/:foo' => sub {...} => 'name';
172 my $route = get '/:foo' => {foo => 'bar'} => sub {...};
173 my $route = get '/:foo' => [foo => qr/\w+/] => sub {...};
174 my $route = get '/:foo' => (agent => qr/Firefox/) => sub {...};
175
176 Generate route with "get" in Mojolicious::Routes::Route, matching only
177 "GET" requests. See Mojolicious::Guides::Tutorial and
178 Mojolicious::Guides::Routing for more information.
179
180 group
181 group {...};
182
183 Start a new route group.
184
185 helper
186 helper foo => sub {...};
187
188 Add a new helper with "helper" in Mojolicious.
189
190 hook
191 hook after_dispatch => sub {...};
192
193 Share code with "hook" in Mojolicious.
194
195 options
196 my $route = options '/:foo' => sub {...};
197 my $route = options '/:foo' => sub {...} => 'name';
198 my $route = options '/:foo' => {foo => 'bar'} => sub {...};
199 my $route = options '/:foo' => [foo => qr/\w+/] => sub {...};
200 my $route = options '/:foo' => (agent => qr/Firefox/) => sub {...};
201
202 Generate route with "options" in Mojolicious::Routes::Route, matching
203 only "OPTIONS" requests. See Mojolicious::Guides::Tutorial and
204 Mojolicious::Guides::Routing for more information.
205
206 patch
207 my $route = patch '/:foo' => sub {...};
208 my $route = patch '/:foo' => sub {...} => 'name';
209 my $route = patch '/:foo' => {foo => 'bar'} => sub {...};
210 my $route = patch '/:foo' => [foo => qr/\w+/] => sub {...};
211 my $route = patch '/:foo' => (agent => qr/Firefox/) => sub {...};
212
213 Generate route with "patch" in Mojolicious::Routes::Route, matching
214 only "PATCH" requests. See Mojolicious::Guides::Tutorial and
215 Mojolicious::Guides::Routing for more information.
216
217 plugin
218 plugin SomePlugin => {foo => 23};
219
220 Load a plugin with "plugin" in Mojolicious.
221
222 post
223 my $route = post '/:foo' => sub {...};
224 my $route = post '/:foo' => sub {...} => 'name';
225 my $route = post '/:foo' => {foo => 'bar'} => sub {...};
226 my $route = post '/:foo' => [foo => qr/\w+/] => sub {...};
227 my $route = post '/:foo' => (agent => qr/Firefox/) => sub {...};
228
229 Generate route with "post" in Mojolicious::Routes::Route, matching only
230 "POST" requests. See Mojolicious::Guides::Tutorial and
231 Mojolicious::Guides::Routing for more information.
232
233 put
234 my $route = put '/:foo' => sub {...};
235 my $route = put '/:foo' => sub {...} => 'name';
236 my $route = put '/:foo' => {foo => 'bar'} => sub {...};
237 my $route = put '/:foo' => [foo => qr/\w+/] => sub {...};
238 my $route = put '/:foo' => (agent => qr/Firefox/) => sub {...};
239
240 Generate route with "put" in Mojolicious::Routes::Route, matching only
241 "PUT" requests. See Mojolicious::Guides::Tutorial and
242 Mojolicious::Guides::Routing for more information.
243
244 under
245 my $route = under sub {...};
246 my $route = under '/:foo' => sub {...};
247 my $route = under '/:foo' => {foo => 'bar'};
248 my $route = under '/:foo' => [foo => qr/\w+/];
249 my $route = under '/:foo' => (agent => qr/Firefox/);
250 my $route = under [format => 0];
251
252 Generate nested route with "under" in Mojolicious::Routes::Route, to
253 which all following routes are automatically appended. See
254 Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more
255 information.
256
257 websocket
258 my $route = websocket '/:foo' => sub {...};
259 my $route = websocket '/:foo' => sub {...} => 'name';
260 my $route = websocket '/:foo' => {foo => 'bar'} => sub {...};
261 my $route = websocket '/:foo' => [foo => qr/\w+/] => sub {...};
262 my $route = websocket '/:foo' => (agent => qr/Firefox/) => sub {...};
263
264 Generate route with "websocket" in Mojolicious::Routes::Route, matching
265 only WebSocket handshakes. See Mojolicious::Guides::Tutorial and
266 Mojolicious::Guides::Routing for more information.
267
269 Mojolicious::Lite inherits all attributes from Mojolicious.
270
272 Mojolicious::Lite inherits all methods from Mojolicious.
273
275 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
276
277
278
279perl v5.32.0 2020-07-28 Mojolicious::Lite(3)