1Mojolicious::Lite(3)  User Contributed Perl Documentation Mojolicious::Lite(3)
2
3
4

NAME

6       Mojolicious::Lite - Micro real-time web framework
7

SYNOPSIS

9         # Automatically enables "strict", "warnings", "utf8" and Perl 5.16 features
10         use Mojolicious::Lite -signatures;
11
12         # Route with placeholder
13         get '/:foo' => sub ($c) {
14           my $foo = $c->param('foo');
15           $c->render(text => "Hello from $foo.");
16         };
17
18         # Start the Mojolicious command system
19         app->start;
20

DESCRIPTION

22       Mojolicious::Lite is a tiny domain specific language built around
23       Mojolicious, made up of only about a dozen Perl functions.
24
25       On Perl 5.20+ you can also use a "-signatures" flag to enable support
26       for subroutine signatures.
27
28         use Mojolicious::Lite -signatures;
29
30         get '/:foo' => sub ($c) {
31           my $foo = $c->param('foo');
32           $c->render(text => "Hello from $foo.");
33         };
34
35         app->start;
36
37       See Mojolicious::Guides::Tutorial for more!
38

GROWING

40       While Mojolicious::Guides::Growing will give you a detailed
41       introduction to growing a Mojolicious::Lite prototype into a well-
42       structured Mojolicious application, here we have collected a few
43       snippets that illustrate very well just how similar both of them are.
44
45   Routes
46       The functions "get", "post" and friends all have equivalent methods.
47
48         # Mojolicious::Lite
49         get '/foo' => sub ($c) {
50           $c->render(text => 'Hello World!');
51         };
52
53         # Mojolicious
54         sub startup ($self) {
55
56           my $routes = $self->routes;
57           $routes->get('/foo' => sub ($c) {
58             $c->render(text => 'Hello World!');
59           });
60         }
61
62   Application
63       The application object you can access with the function "app" is the
64       first argument passed to the "startup" method.
65
66         # Mojolicious::Lite
67         app->max_request_size(16777216);
68
69         # Mojolicious
70         sub startup ($self) {
71           $self->max_request_size(16777216);
72         }
73
74   Plugins
75       Instead of the "plugin" function you just use the method "plugin" in
76       Mojolicious.
77
78         # Mojolicious::Lite
79         plugin 'Config';
80
81         # Mojolicious
82         sub startup ($self) {
83           $self->plugin('Config');
84         }
85
86   Helpers
87       Similar to plugins, instead of the "helper" function you just use the
88       method "helper" in Mojolicious.
89
90         # Mojolicious::Lite
91         helper two => sub ($c) {
92           return 1 + 1;
93         };
94
95         # Mojolicious
96         sub startup ($self) {
97           $self->helper(two => sub ($c) {
98             return 1 + 1;
99           });
100         }
101
102   Under
103       Instead of sequential function calls, we can use methods to build a
104       tree with nested routes, that much better illustrates how routes work
105       internally.
106
107         # Mojolicious::Lite
108         under '/foo';
109         get '/bar' => sub ($c) {...};
110
111         # Mojolicious
112         sub startup ($self) {
113
114           my $routes = $self->routes;
115           my $foo = $routes->under('/foo');
116           $foo->get('/bar' => sub ($c) {...});
117         }
118

FUNCTIONS

120       Mojolicious::Lite implements the following functions, which are
121       automatically exported.
122
123   any
124         my $route = any '/:foo' => sub ($c) {...};
125         my $route = any '/:foo' => sub ($c) {...} => 'name';
126         my $route = any '/:foo' => {foo => 'bar'} => sub ($c) {...};
127         my $route = any '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
128         my $route = any ['GET', 'POST'] => '/:foo' => sub ($c) {...};
129         my $route = any ['GET', 'POST'] => '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
130         my $route = any ['GET', 'POST'] => '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
131
132       Generate route with "any" in Mojolicious::Routes::Route, matching any
133       of the listed HTTP request methods or all. See
134       Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more
135       information.
136
137   app
138         my $app = app;
139
140       Returns the Mojolicious::Lite application object, which is a subclass
141       of Mojolicious.
142
143         # Use all the available attributes and methods
144         app->log->level('error');
145         app->defaults(foo => 'bar');
146
147   del
148         my $route = del '/:foo' => sub ($c) {...};
149         my $route = del '/:foo' => sub ($c) {...} => 'name';
150         my $route = del '/:foo' => {foo => 'bar'} => sub ($c) {...};
151         my $route = del '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
152         my $route = del '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
153
154       Generate route with "delete" in Mojolicious::Routes::Route, matching
155       only "DELETE" requests. See Mojolicious::Guides::Tutorial and
156       Mojolicious::Guides::Routing for more information.
157
158   get
159         my $route = get '/:foo' => sub ($c) {...};
160         my $route = get '/:foo' => sub ($c) {...} => 'name';
161         my $route = get '/:foo' => {foo => 'bar'} => sub ($c) {...};
162         my $route = get '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
163         my $route = get '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
164
165       Generate route with "get" in Mojolicious::Routes::Route, matching only
166       "GET" requests. See Mojolicious::Guides::Tutorial and
167       Mojolicious::Guides::Routing for more information.
168
169   group
170         group {...};
171
172       Start a new route group.
173
174   helper
175         helper foo => sub ($c, @args) {...};
176
177       Add a new helper with "helper" in Mojolicious.
178
179   hook
180         hook after_dispatch => sub ($c) {...};
181
182       Share code with "hook" in Mojolicious.
183
184   options
185         my $route = options '/:foo' => sub ($c) {...};
186         my $route = options '/:foo' => sub ($c) {...} => 'name';
187         my $route = options '/:foo' => {foo => 'bar'} => sub ($c) {...};
188         my $route = options '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
189         my $route = options '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
190
191       Generate route with "options" in Mojolicious::Routes::Route, matching
192       only "OPTIONS" requests. See Mojolicious::Guides::Tutorial and
193       Mojolicious::Guides::Routing for more information.
194
195   patch
196         my $route = patch '/:foo' => sub ($c) {...};
197         my $route = patch '/:foo' => sub ($c) {...} => 'name';
198         my $route = patch '/:foo' => {foo => 'bar'} => sub ($c) {...};
199         my $route = patch '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
200         my $route = patch '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
201
202       Generate route with "patch" in Mojolicious::Routes::Route, matching
203       only "PATCH" requests. See Mojolicious::Guides::Tutorial and
204       Mojolicious::Guides::Routing for more information.
205
206   plugin
207         plugin SomePlugin => {foo => 23};
208
209       Load a plugin with "plugin" in Mojolicious.
210
211   post
212         my $route = post '/:foo' => sub ($c) {...};
213         my $route = post '/:foo' => sub ($c) {...} => 'name';
214         my $route = post '/:foo' => {foo => 'bar'} => sub ($c) {...};
215         my $route = post '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
216         my $route = post '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
217
218       Generate route with "post" in Mojolicious::Routes::Route, matching only
219       "POST" requests. See Mojolicious::Guides::Tutorial and
220       Mojolicious::Guides::Routing for more information.
221
222   put
223         my $route = put '/:foo' => sub ($c) {...};
224         my $route = put '/:foo' => sub ($c) {...} => 'name';
225         my $route = put '/:foo' => {foo => 'bar'} => sub ($c) {...};
226         my $route = put '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
227         my $route = put '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
228
229       Generate route with "put" in Mojolicious::Routes::Route, matching only
230       "PUT" requests. See Mojolicious::Guides::Tutorial and
231       Mojolicious::Guides::Routing for more information.
232
233   under
234         my $route = under sub ($c) {...};
235         my $route = under '/:foo' => sub ($c) {...};
236         my $route = under '/:foo' => {foo => 'bar'};
237         my $route = under '/:foo' => [foo => qr/\w+/];
238         my $route = under '/:foo' => (agent => qr/Firefox/);
239         my $route = under [format => 0];
240
241       Generate nested route with "under" in Mojolicious::Routes::Route, to
242       which all following routes are automatically appended. See
243       Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more
244       information.
245
246   websocket
247         my $route = websocket '/:foo' => sub ($c) {...};
248         my $route = websocket '/:foo' => sub ($c) {...} => 'name';
249         my $route = websocket '/:foo' => {foo => 'bar'} => sub ($c) {...};
250         my $route = websocket '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
251         my $route = websocket '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
252
253       Generate route with "websocket" in Mojolicious::Routes::Route, matching
254       only WebSocket handshakes. See Mojolicious::Guides::Tutorial and
255       Mojolicious::Guides::Routing for more information.
256

ATTRIBUTES

258       Mojolicious::Lite inherits all attributes from Mojolicious.
259

METHODS

261       Mojolicious::Lite inherits all methods from Mojolicious.
262

SEE ALSO

264       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
265
266
267
268perl v5.32.1                      2021-02-07              Mojolicious::Lite(3)
Impressum