Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 902 lines (645 sloc) 21.291 kb
a146626 added Mojolicious::Lite
sri authored
1 package Mojolicious::Lite;
3c2998b @kraih added experimental much prettier attribute generator to Mojo::Base
authored
2 use Mojo::Base 'Mojolicious';
a146626 added Mojolicious::Lite
sri authored
3
ad7827d @kraih micro optimization
authored
4 # Lite apps are modern!
5 require feature if $] >= 5.010;
6
388f1aa @kraih made simpsons and futurama quotes easier to distinguish from normal comm...
authored
7 # "Since when is the Internet all about robbing people of their privacy?
8 # August 6, 1991."
8a7633c @kraih fixed a few home detection bugs
authored
9 use File::Basename 'dirname';
a146626 added Mojolicious::Lite
sri authored
10 use File::Spec;
11
388f1aa @kraih made simpsons and futurama quotes easier to distinguish from normal comm...
authored
12 # "It's the future, my parents, my co-workers, my girlfriend,
13 # I'll never see any of them ever again... YAHOOO!"
a146626 added Mojolicious::Lite
sri authored
14 sub import {
e8805d1 @kraih changed indentation style
authored
15 my $class = shift;
16
17 # Executable
18 $ENV{MOJO_EXE} ||= (caller)[1];
19
20 # Home
21 local $ENV{MOJO_HOME} =
dfccce0 @kraih Revert "improved .perltidyrc"
authored
22 File::Spec->catdir(split '/', dirname($ENV{MOJO_EXE}))
23 unless $ENV{MOJO_HOME};
e8805d1 @kraih changed indentation style
authored
24
25 # Initialize app
26 my $app = $class->new;
27
28 # Initialize routes
29 my $routes = $app->routes;
30 $routes->namespace('');
31
32 # Prepare exports
33 my $caller = caller;
34 no strict 'refs';
35 no warnings 'redefine';
36
37 # Default static and template class
38 $app->static->default_static_class($caller);
39 $app->renderer->default_template_class($caller);
40
41 # Export
507ece2 @kraih improved a few more comments
authored
42 my $root = $routes;
e8805d1 @kraih changed indentation style
authored
43 *{"${caller}::new"} = *{"${caller}::app"} = sub {$app};
9deddc7 @kraih added experimental helper function to Mojolicious::Lite
authored
44 *{"${caller}::any"} = sub { $routes->any(@_) };
45 *{"${caller}::del"} = sub { $routes->del(@_) };
46 *{"${caller}::get"} = sub { $routes->get(@_) };
47 *{"${caller}::helper"} = sub { $app->helper(@_) };
48 *{"${caller}::hook"} = sub { $app->hook(@_) };
49 *{"${caller}::under"} = *{"${caller}::ladder"} =
dfccce0 @kraih Revert "improved .perltidyrc"
authored
50 sub { $routes = $root->under(@_) };
e8805d1 @kraih changed indentation style
authored
51 *{"${caller}::plugin"} = sub { $app->plugin(@_) };
52 *{"${caller}::post"} = sub { $routes->post(@_) };
53 *{"${caller}::put"} = sub { $routes->put(@_) };
54 *{"${caller}::websocket"} = sub { $routes->websocket(@_) };
55
56 # We are most likely the app in a lite environment
57 $ENV{MOJO_APP} ||= $app;
58
59 # Shagadelic!
60 *{"${caller}::shagadelic"} = sub { $app->start(@_) };
1464866 @kraih reuse Mojo::Base in Mojolicious::Lite
authored
61
62 # Lite apps are strict!
63 Mojo::Base->import(-strict);
a146626 added Mojolicious::Lite
sri authored
64 }
65
66 1;
67 __END__
68
69 =head1 NAME
70
71 Mojolicious::Lite - Micro Web Framework
72
73 =head1 SYNOPSIS
74
e8805d1 @kraih changed indentation style
authored
75 # Using Mojolicious::Lite will enable "strict" and "warnings"
76 use Mojolicious::Lite;
a146626 added Mojolicious::Lite
sri authored
77
e8805d1 @kraih changed indentation style
authored
78 # Route with placeholder
79 get '/:foo' => sub {
80 my $self = shift;
81 my $foo = $self->param('foo');
82 $self->render(text => "Hello from $foo!");
83 };
a146626 added Mojolicious::Lite
sri authored
84
e8805d1 @kraih changed indentation style
authored
85 # Start the Mojolicious command system
86 app->start;
a146626 added Mojolicious::Lite
sri authored
87
124ea7e reformatted lite examples to a tutorial
sri authored
88 =head1 DESCRIPTION
89
04d0204 @cowens fixed typo
cowens authored
90 L<Mojolicious::Lite> is a micro web framework built around L<Mojolicious>.
124ea7e reformatted lite examples to a tutorial
sri authored
91
ef8a00e @kraih ho ho ho
authored
92 =head1 TUTORIAL
93
a064263 @kraih ho ho ho
authored
94 A quick example driven introduction to the wonders of L<Mojolicious::Lite>.
95 Most of what you'll learn here also applies to normal L<Mojolicious>
96 applications.
ef8a00e @kraih ho ho ho
authored
97
98 =head2 Hello World!
99
ff9a129 @kraih small documentation tweak
authored
100 A simple Hello World application can look like this, L<strict> and
101 L<warnings> are automatically enabled and a few functions imported when you
102 use L<Mojolicious::Lite>, turning your script into a full featured web
3f8a86e added basic http proxy support
Sebastian Riedel authored
103 application.
9b87480 improved tutorial a bit
sri authored
104
e8805d1 @kraih changed indentation style
authored
105 #!/usr/bin/env perl
106 use Mojolicious::Lite;
9b87480 improved tutorial a bit
sri authored
107
2c1834e @kraih better hello world examples
authored
108 get '/' => sub {
109 my $self = shift;
110 $self->render(text => 'Hello World!');
111 };
9b87480 improved tutorial a bit
sri authored
112
e8805d1 @kraih changed indentation style
authored
113 app->start;
9b87480 improved tutorial a bit
sri authored
114
ef8a00e @kraih ho ho ho
authored
115 =head2 Generator
116
9c69cc4 renamed script system to command system
Sebastian Riedel authored
117 There is also a helper command to generate a small example application.
124ea7e reformatted lite examples to a tutorial
sri authored
118
951cb5a @kraih documentation tweaks
authored
119 $ mojo generate lite_app
124ea7e reformatted lite examples to a tutorial
sri authored
120
ef8a00e @kraih ho ho ho
authored
121 =head2 Commands
122
dc16049 @kraih improved documentation
authored
123 All the normal L<Mojolicious::Commands> are available from the command line.
58efd9e cleanup
Sebastian Riedel authored
124 Note that CGI, FastCGI and PSGI environments can usually be auto detected and
125 will just work without commands.
124ea7e reformatted lite examples to a tutorial
sri authored
126
951cb5a @kraih documentation tweaks
authored
127 $ ./myapp.pl daemon
e8805d1 @kraih changed indentation style
authored
128 Server available at http://127.0.0.1:3000.
124ea7e reformatted lite examples to a tutorial
sri authored
129
951cb5a @kraih documentation tweaks
authored
130 $ ./myapp.pl daemon --listen http://*:8080
e8805d1 @kraih changed indentation style
authored
131 Server available at http://127.0.0.1:8080.
124ea7e reformatted lite examples to a tutorial
sri authored
132
951cb5a @kraih documentation tweaks
authored
133 $ ./myapp.pl cgi
e8805d1 @kraih changed indentation style
authored
134 ...CGI output...
124ea7e reformatted lite examples to a tutorial
sri authored
135
951cb5a @kraih documentation tweaks
authored
136 $ ./myapp.pl fastcgi
e8805d1 @kraih changed indentation style
authored
137 ...Blocking FastCGI main loop...
24b3bff added ->render(text => "") support and many lite examples
sri authored
138
951cb5a @kraih documentation tweaks
authored
139 $ ./myapp.pl
e8805d1 @kraih changed indentation style
authored
140 ...List of available commands (or automatically detected environment)...
3f8a86e added basic http proxy support
Sebastian Riedel authored
141
ef8a00e @kraih ho ho ho
authored
142 =head2 Start
143
80f8c48 switched to app->start instead of shagadelic as default way to start Moj...
Sebastian Riedel authored
144 The app->start call that starts the L<Mojolicious> command system can be
124ea7e reformatted lite examples to a tutorial
sri authored
145 customized to override normal C<@ARGV> use.
146
e8805d1 @kraih changed indentation style
authored
147 app->start('cgi');
24b3bff added ->render(text => "") support and many lite examples
sri authored
148
ef8a00e @kraih ho ho ho
authored
149 =head2 Reloading
150
5af1133 @kraih added experimental self-restarting Morbo development web server and remo...
authored
151 Your application will automatically reload itself if you start it with the
bf20f9b @kraih improved Morbo documentation and fixed a small bug in Mojo::Parameters
authored
152 C<morbo> development web server, so you don't have to restart the server
153 after every change.
2e9c57b added MOJO_RELOAD section to Mojolicious::Lite tutorial
Sebastian Riedel authored
154
951cb5a @kraih documentation tweaks
authored
155 $ morbo myapp.pl
e8805d1 @kraih changed indentation style
authored
156 Server available at http://127.0.0.1:3000.
2e9c57b added MOJO_RELOAD section to Mojolicious::Lite tutorial
Sebastian Riedel authored
157
ef8a00e @kraih ho ho ho
authored
158 =head2 Routes
159
c564032 fixed typo
sri authored
160 Routes are basically just fancy paths that can contain different kinds of
124ea7e reformatted lite examples to a tutorial
sri authored
161 placeholders.
fdd42b5 @kraih improved Mojolicious::Lite tutorial
authored
162 C<$self> is an instance of L<Mojolicious::Controller> containing both the
163 HTTP request and response.
124ea7e reformatted lite examples to a tutorial
sri authored
164
e8805d1 @kraih changed indentation style
authored
165 # /foo
166 get '/foo' => sub {
167 my $self = shift;
168 $self->render(text => 'Hello World!');
169 };
0b889b9 added more examples
sri authored
170
19f6f6d @kraih added file upload example to lite tutorial
authored
171 =head2 GET/POST Parameters
4c166bf @kraih added GET/POST parameter example to lite tutorial
authored
172
173 All C<GET> and C<POST> parameters are accessible via C<param>.
174
175 # /foo?user=sri
176 get '/foo' => sub {
177 my $self = shift;
178 my $user = $self->param('user');
179 $self->render(text => "Hello $user!");
180 };
181
ef8a00e @kraih ho ho ho
authored
182 =head2 Stash
183
229c79d @kraih cleanup
authored
184 The C<stash> is used to pass data to templates, which can be inlined in the
185 C<DATA> section.
d7f702a @kraih added recipe
authored
186
e8805d1 @kraih changed indentation style
authored
187 # /bar
188 get '/bar' => sub {
189 my $self = shift;
190 $self->stash(one => 23);
191 $self->render('baz', two => 24);
192 };
d7f702a @kraih added recipe
authored
193
e8805d1 @kraih changed indentation style
authored
194 __DATA__
d7f702a @kraih added recipe
authored
195
e8805d1 @kraih changed indentation style
authored
196 @@ baz.html.ep
197 The magic numbers are <%= $one %> and <%= $two %>.
d7f702a @kraih added recipe
authored
198
a66add4 @kraih added recipe to lite tutorial
authored
199 =head2 HTTP
200
201 L<Mojo::Message::Request> and L<Mojo::Message::Response> give you full access
202 to all HTTP features and information.
203
204 # /agent
205 get '/agent' => sub {
206 my $self = shift;
207 $self->res->headers->header('X-Bender' => 'Bite my shiny metal ass!');
208 $self->render(text => $self->req->headers->user_agent);
209 };
210
ef8a00e @kraih ho ho ho
authored
211 =head2 Route Names
212
124ea7e reformatted lite examples to a tutorial
sri authored
213 All routes can have a name associated with them, this allows automatic
4b293ef updated tutorial with tag helpers
Sebastian Riedel authored
214 template detection and back referencing with C<url_for>, C<link_to> and
215 C<form_for>.
832d45d @kraih deprecated wildcard route names in favor of automatically generated defa...
authored
216 Nameless routes get an automatically generated one assigned that is simply
217 equal to the route itself without non-word characters.
124ea7e reformatted lite examples to a tutorial
sri authored
218
e8805d1 @kraih changed indentation style
authored
219 # /
220 get '/' => 'index';
124ea7e reformatted lite examples to a tutorial
sri authored
221
e8805d1 @kraih changed indentation style
authored
222 # /hello
223 get '/hello';
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
224
e8805d1 @kraih changed indentation style
authored
225 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
226
e8805d1 @kraih changed indentation style
authored
227 @@ index.html.ep
228 <%= link_to Hello => 'hello' %>.
229 <%= link_to Reload => 'index' %>.
124ea7e reformatted lite examples to a tutorial
sri authored
230
e8805d1 @kraih changed indentation style
authored
231 @@ hello.html.ep
232 Hello World!
9ac9059 added more lite examples
sri authored
233
ef8a00e @kraih ho ho ho
authored
234 =head2 Layouts
235
124ea7e reformatted lite examples to a tutorial
sri authored
236 Templates can have layouts.
237
0528af7 @kraih a few more documentation tweaks
authored
238 # /with_layout
e8805d1 @kraih changed indentation style
authored
239 get '/with_layout' => sub {
240 my $self = shift;
241 $self->render('with_layout');
242 };
124ea7e reformatted lite examples to a tutorial
sri authored
243
e8805d1 @kraih changed indentation style
authored
244 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
245
e8805d1 @kraih changed indentation style
authored
246 @@ with_layout.html.ep
247 % title 'Green!';
248 % layout 'green';
249 We've got content!
124ea7e reformatted lite examples to a tutorial
sri authored
250
e8805d1 @kraih changed indentation style
authored
251 @@ layouts/green.html.ep
252 <!doctype html><html>
253 <head><title><%= title %></title></head>
254 <body><%= content %></body>
255 </html>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
256
ef8a00e @kraih ho ho ho
authored
257 =head2 Blocks
258
e385caf @kraih pod cleanup
authored
259 Template blocks can be used like normal Perl functions and are always
260 delimited by the C<begin> and C<end> keywords.
ec39ab1 added tutorial example for blocks
Sebastian Riedel authored
261
0528af7 @kraih a few more documentation tweaks
authored
262 # /with_block
e8805d1 @kraih changed indentation style
authored
263 get '/with_block' => 'block';
ec39ab1 added tutorial example for blocks
Sebastian Riedel authored
264
e8805d1 @kraih changed indentation style
authored
265 __DATA__
ec39ab1 added tutorial example for blocks
Sebastian Riedel authored
266
e8805d1 @kraih changed indentation style
authored
267 @@ block.html.ep
268 <% my $link = begin %>
269 <% my ($url, $name) = @_; %>
270 Try <%= link_to $url => begin %><%= $name %><% end %>!
271 <% end %>
272 <!doctype html><html>
273 <head><title>Sebastians Frameworks!</title></head>
274 <body>
a13e7e5 @kraih fixed a bug in Mojo::Template that caused template blocks to be auto esc...
authored
275 <%= $link->('http://mojolicio.us', 'Mojolicious') %>
276 <%= $link->('http://catalystframework.org', 'Catalyst') %>
e8805d1 @kraih changed indentation style
authored
277 </body>
278 </html>
ec39ab1 added tutorial example for blocks
Sebastian Riedel authored
279
ef8a00e @kraih ho ho ho
authored
280 =head2 Captured Content
281
e391bef @kraih added experimental content_for helper
authored
282 The C<content_for> helper can be used to pass around blocks of captured
283 content.
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
284
0528af7 @kraih a few more documentation tweaks
authored
285 # /captured
e8805d1 @kraih changed indentation style
authored
286 get '/captured' => sub {
287 my $self = shift;
288 $self->render('captured');
289 };
290
291 __DATA__
292
293 @@ captured.html.ep
294 % layout 'blue', title => 'Green!';
295 <% content_for header => begin %>
296 <meta http-equiv="Pragma" content="no-cache">
297 <% end %>
298 We've got content!
299 <% content_for header => begin %>
300 <meta http-equiv="Expires" content="-1">
301 <% end %>
302
303 @@ layouts/blue.html.ep
304 <!doctype html><html>
305 <head>
306 <title><%= title %></title>
307 <%= content_for 'header' %>
308 </head>
309 <body><%= content %></body>
310 </html>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
311
ef8a00e @kraih ho ho ho
authored
312 =head2 Helpers
313
07da3ee @kraih ho ho ho
authored
314 You can also extend L<Mojolicious> with your own helpers, a list of all built
315 in ones can be found in L<Mojolicious::Plugin::DefaultHelpers> and
316 L<Mojolicious::Plugin::TagHelpers>.
317
e8805d1 @kraih changed indentation style
authored
318 # "whois" helper
9deddc7 @kraih added experimental helper function to Mojolicious::Lite
authored
319 helper whois => sub {
e8805d1 @kraih changed indentation style
authored
320 my $self = shift;
321 my $agent = $self->req->headers->user_agent || 'Anonymous';
322 my $ip = $self->tx->remote_address;
323 return "$agent ($ip)";
9deddc7 @kraih added experimental helper function to Mojolicious::Lite
authored
324 };
07da3ee @kraih ho ho ho
authored
325
0528af7 @kraih a few more documentation tweaks
authored
326 # /secret
e8805d1 @kraih changed indentation style
authored
327 get '/secret' => sub {
328 my $self = shift;
329 my $user = $self->whois;
330 $self->app->log->debug("Request from $user.");
331 };
07da3ee @kraih ho ho ho
authored
332
e8805d1 @kraih changed indentation style
authored
333 __DATA__
07da3ee @kraih ho ho ho
authored
334
e8805d1 @kraih changed indentation style
authored
335 @@ secret.html.ep
336 We know who you are <%= whois %>.
07da3ee @kraih ho ho ho
authored
337
ef8a00e @kraih ho ho ho
authored
338 =head2 Placeholders
339
124ea7e reformatted lite examples to a tutorial
sri authored
340 Route placeholders allow capturing parts of a request path until a C</> or
5de8c36 added routes captures to params in Mojolicious
Sebastian Riedel authored
341 C<.> separator occurs, results will be stored by name in the C<stash> and
342 C<param>.
124ea7e reformatted lite examples to a tutorial
sri authored
343
e8805d1 @kraih changed indentation style
authored
344 # /foo/test
345 # /foo/test123
346 get '/foo/:bar' => sub {
347 my $self = shift;
348 my $bar = $self->stash('bar');
349 $self->render(text => "Our :bar placeholder matched $bar");
350 };
351
352 # /test/foo
353 # /test123/foo
354 get '/(:bar)something/foo' => sub {
355 my $self = shift;
356 my $bar = $self->param('bar');
357 $self->render(text => "Our :bar placeholder matched $bar");
358 };
24b3bff added ->render(text => "") support and many lite examples
sri authored
359
ef8a00e @kraih ho ho ho
authored
360 =head2 Wildcard Placeholders
361
124ea7e reformatted lite examples to a tutorial
sri authored
362 Wildcard placeholders allow matching absolutely everything, including
363 C</> and C<.>.
364
e8805d1 @kraih changed indentation style
authored
365 # /hello/test
366 # /hello/test123
367 # /hello/test.123/test/123
a54cb81 @kraih added experimental support for unquoted wildcard placeholders in Mojolic...
authored
368 get '/hello/*you' => sub {
e8805d1 @kraih changed indentation style
authored
369 shift->render('groovy');
370 };
19499ca fixed typo
sri authored
371
e8805d1 @kraih changed indentation style
authored
372 __DATA__
19499ca fixed typo
sri authored
373
e8805d1 @kraih changed indentation style
authored
374 @@ groovy.html.ep
375 Your name is <%= $you %>.
885a5e8 added more lite examples
sri authored
376
ef8a00e @kraih ho ho ho
authored
377 =head2 HTTP Methods
378
124ea7e reformatted lite examples to a tutorial
sri authored
379 Routes can be restricted to specific request methods.
380
e8805d1 @kraih changed indentation style
authored
381 # GET /bye
382 get '/bye' => sub { shift->render(text => 'Bye!') };
124ea7e reformatted lite examples to a tutorial
sri authored
383
e8805d1 @kraih changed indentation style
authored
384 # POST /bye
385 post '/bye' => sub { shift->render(text => 'Bye!') };
124ea7e reformatted lite examples to a tutorial
sri authored
386
e8805d1 @kraih changed indentation style
authored
387 # GET|POST|DELETE /bye
388 any [qw/get post delete/] => '/bye' => sub {
389 shift->render(text => 'Bye!');
390 };
124ea7e reformatted lite examples to a tutorial
sri authored
391
0528af7 @kraih a few more documentation tweaks
authored
392 # * /baz
e8805d1 @kraih changed indentation style
authored
393 any '/baz' => sub {
394 my $self = shift;
395 my $method = $self->req->method;
396 $self->render(text => "You called /baz with $method");
397 };
124ea7e reformatted lite examples to a tutorial
sri authored
398
b9f5641 @kraih added more documentation
authored
399 =head2 Optional Placeholders
400
401 Routes allow default values to make placeholders optional.
402
403 # /hello
404 # /hello/Sara
405 get '/hello/:name' => {name => 'Sebastian'} => sub {
406 my $self = shift;
407 $self->render('groovy', format => 'txt');
408 };
409
410 __DATA__
411
412 @@ groovy.txt.ep
413 My name is <%= $name %>.
414
415 =head2 Restrictive Placeholders
ef8a00e @kraih ho ho ho
authored
416
b9f5641 @kraih added more documentation
authored
417 The easiest way to make placeholders more restrictive are alternatives, you
418 just make a list of possible values.
124ea7e reformatted lite examples to a tutorial
sri authored
419
95e5686 @kraih added experimental support for simple alternative constraints to routes
authored
420 # /test
013ad05 @kraih improved route examples
authored
421 # /123
95e5686 @kraih added experimental support for simple alternative constraints to routes
authored
422 any '/:foo' => [foo => [qw/test 123/]] => sub {
e8805d1 @kraih changed indentation style
authored
423 my $self = shift;
424 my $foo = $self->param('foo');
425 $self->render(text => "Our :foo placeholder matched $foo");
426 };
17220cb @kraih improved example
authored
427
b9f5641 @kraih added more documentation
authored
428 All placeholders get compiled to a regex internally, this process can also be
429 easily customized.
95e5686 @kraih added experimental support for simple alternative constraints to routes
authored
430
431 # /1
432 # /123
433 any '/:bar' => [bar => qr/\d+/] => sub {
e8805d1 @kraih changed indentation style
authored
434 my $self = shift;
435 my $bar = $self->param('bar');
436 $self->render(text => "Our :bar placeholder matched $bar");
437 };
24b3bff added ->render(text => "") support and many lite examples
sri authored
438
bb870df @kraih pod improvements
authored
439 Just make sure not to use C<^> and C<$> or capturing groups C<(...)>, because
440 placeholders become part of a larger regular expression internally,
441 C<(?:...)> is fine though.
442
b9f5641 @kraih added more documentation
authored
443 =head2 Formats
ef8a00e @kraih ho ho ho
authored
444
b9f5641 @kraih added more documentation
authored
445 Formats can be automatically detected by looking at file extensions.
124ea7e reformatted lite examples to a tutorial
sri authored
446
b9f5641 @kraih added more documentation
authored
447 # /detection.html
448 # /detection.txt
449 get '/detection' => sub {
e8805d1 @kraih changed indentation style
authored
450 my $self = shift;
b9f5641 @kraih added more documentation
authored
451 $self->render('detected');
e8805d1 @kraih changed indentation style
authored
452 };
124ea7e reformatted lite examples to a tutorial
sri authored
453
e8805d1 @kraih changed indentation style
authored
454 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
455
b9f5641 @kraih added more documentation
authored
456 @@ detected.html.ep
457 <!doctype html><html>
458 <head><title>Detected!</title></head>
459 <body>HTML was detected.</body>
460 </html>
124ea7e reformatted lite examples to a tutorial
sri authored
461
b9f5641 @kraih added more documentation
authored
462 @@ detected.txt.ep
463 TXT was detected.
124ea7e reformatted lite examples to a tutorial
sri authored
464
b9f5641 @kraih added more documentation
authored
465 Restrictive placeholders can also be used for format detection.
124ea7e reformatted lite examples to a tutorial
sri authored
466
b9f5641 @kraih added more documentation
authored
467 # /hello.json
468 # /hello.txt
469 get '/hello' => [format => [qw/json txt/]] => sub {
e8805d1 @kraih changed indentation style
authored
470 my $self = shift;
3ef9cbc @kraih fixed example
authored
471 return $self->render_json({hello => 'world!'})
472 if $self->stash('format') eq 'json';
b9f5641 @kraih added more documentation
authored
473 $self->render_text('hello world!');
474 };
124ea7e reformatted lite examples to a tutorial
sri authored
475
f6a1684 @kraih better description
authored
476 =head2 Content Negotiation
ef80a73 @kraih added experimental extended REST support to Mojolicious
authored
477
d97715a @kraih added content negotiation section to rendering guide
authored
478 For resources with different representations and that require truly
479 C<RESTful> content negotiation you can also use C<respond_to>.
ef80a73 @kraih added experimental extended REST support to Mojolicious
authored
480
481 # /hello (Accept: application/json)
482 # /hello (Accept: text/xml)
483 # /hello.json
484 # /hello.xml
485 get '/hello' => sub {
486 my $self = shift;
487 $self->respond_to(
2022573 @kraih added many more features to respond_to
authored
488 json => {json => {hello => 'world'}},
e04d769 @kraih better respond_to examples
authored
489 xml => {text => '<hello>world</hello>'},
2022573 @kraih added many more features to respond_to
authored
490 any => {data => '', status => 204}
491 );
ef80a73 @kraih added experimental extended REST support to Mojolicious
authored
492 };
493
ef8a00e @kraih ho ho ho
authored
494 =head2 Under
495
3f8a86e added basic http proxy support
Sebastian Riedel authored
496 Authentication and code shared between multiple routes can be realized easily
497 with the C<under> statement.
498 All following routes are only evaluated if the C<under> callback returned a
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
499 true value.
500
e8805d1 @kraih changed indentation style
authored
501 use Mojolicious::Lite;
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
502
e8805d1 @kraih changed indentation style
authored
503 # Authenticate based on name parameter
504 under sub {
505 my $self = shift;
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
506
e8805d1 @kraih changed indentation style
authored
507 # Authenticated
508 my $name = $self->param('name') || '';
509 return 1 if $name eq 'Bender';
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
510
e8805d1 @kraih changed indentation style
authored
511 # Not authenticated
512 $self->render('denied');
513 return;
514 };
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
515
0528af7 @kraih a few more documentation tweaks
authored
516 # / (with authentication)
e8805d1 @kraih changed indentation style
authored
517 get '/' => 'index';
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
518
e8805d1 @kraih changed indentation style
authored
519 app->start;
520 __DATA__;
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
521
e8805d1 @kraih changed indentation style
authored
522 @@ denied.html.ep
523 You are not Bender, permission denied!
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
524
e8805d1 @kraih changed indentation style
authored
525 @@ index.html.ep
526 Hi Bender!
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
527
5a92724 fixed a small under bug in Mojolicious::Lite
Sebastian Riedel authored
528 Prefixing multiple routes is another good use for C<under>.
529
e8805d1 @kraih changed indentation style
authored
530 use Mojolicious::Lite;
5a92724 fixed a small under bug in Mojolicious::Lite
Sebastian Riedel authored
531
e8805d1 @kraih changed indentation style
authored
532 # /foo
533 under '/foo';
5a92724 fixed a small under bug in Mojolicious::Lite
Sebastian Riedel authored
534
0528af7 @kraih a few more documentation tweaks
authored
535 # /foo/bar
e8805d1 @kraih changed indentation style
authored
536 get '/bar' => sub { shift->render(text => 'bar!') };
5a92724 fixed a small under bug in Mojolicious::Lite
Sebastian Riedel authored
537
0528af7 @kraih a few more documentation tweaks
authored
538 # /foo/baz
e8805d1 @kraih changed indentation style
authored
539 get '/baz' => sub { shift->render(text => 'baz!') };
5a92724 fixed a small under bug in Mojolicious::Lite
Sebastian Riedel authored
540
e8805d1 @kraih changed indentation style
authored
541 app->start;
5a92724 fixed a small under bug in Mojolicious::Lite
Sebastian Riedel authored
542
ef8a00e @kraih ho ho ho
authored
543 =head2 Conditions
544
f055986 @kraih improved documentation for conditions again
authored
545 Conditions such as C<agent> and C<host> from
546 L<Mojolicious::Plugin::HeaderCondition> allow even more powerful route
9d222db @kraih more documentation for conditions
authored
547 constructs.
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
548
e8805d1 @kraih changed indentation style
authored
549 # /foo
550 get '/foo' => (agent => qr/Firefox/) => sub {
551 shift->render(text => 'Congratulations, you are using a cool browser!');
552 };
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
553
e8805d1 @kraih changed indentation style
authored
554 # /foo
555 get '/foo' => (agent => qr/Internet Explorer/) => sub {
556 shift->render(text => 'Dude, you really need to upgrade to Firefox!');
557 };
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
558
9d222db @kraih more documentation for conditions
authored
559 # /bar
560 get '/bar' => (host => 'mojolicio.us') => sub {
561 shift->render(text => 'Hello Mojolicious!');
562 };
563
ef8a00e @kraih ho ho ho
authored
564 =head2 Sessions
565
15a91a4 pod cleanup
Sebastian Riedel authored
566 Signed cookie based sessions just work out of the box as soon as you start
567 using them.
0851ec6 cleanup
Sebastian Riedel authored
568 The C<flash> can be used to store values that will only be available for the
569 next request (unlike C<stash>, which is only available for the current
570 request), this is very useful in combination with C<redirect_to>.
9fcb9d0 added login example to the tutorial demonstrating sessions
Sebastian Riedel authored
571
e8805d1 @kraih changed indentation style
authored
572 use Mojolicious::Lite;
573
574 get '/login' => sub {
575 my $self = shift;
0f75f9f @kraih documentation tweaks
authored
576
e8805d1 @kraih changed indentation style
authored
577 my $name = $self->param('name') || '';
578 my $pass = $self->param('pass') || '';
579 return $self->render unless $name eq 'sebastian' && $pass eq '1234';
0f75f9f @kraih documentation tweaks
authored
580
e8805d1 @kraih changed indentation style
authored
581 $self->session(name => $name);
582 $self->flash(message => 'Thanks for logging in!');
583 $self->redirect_to('index');
0f75f9f @kraih documentation tweaks
authored
584 };
e8805d1 @kraih changed indentation style
authored
585
586 get '/' => sub {
587 my $self = shift;
0f75f9f @kraih documentation tweaks
authored
588 $self->redirect_to('login') unless $self->session('name');
e8805d1 @kraih changed indentation style
authored
589 } => 'index';
590
591 get '/logout' => sub {
592 my $self = shift;
593 $self->session(expires => 1);
594 $self->redirect_to('index');
0f75f9f @kraih documentation tweaks
authored
595 };
e8805d1 @kraih changed indentation style
authored
596
597 app->start;
598 __DATA__
599
600 @@ layouts/default.html.ep
601 <!doctype html><html>
602 <head><title><%= title %></title></head>
603 <body><%= content %></body>
604 </html>
605
606 @@ login.html.ep
607 % layout 'default';
608 % title 'Login';
609 <%= form_for login => begin %>
610 <% if (param 'name') { %>
611 <b>Wrong name or password, please try again.</b><br>
b42a2c8 also use flash in the session example
Sebastian Riedel authored
612 <% } %>
e8805d1 @kraih changed indentation style
authored
613 Name:<br>
614 <%= text_field 'name' %><br>
615 Password:<br>
616 <%= password_field 'pass' %><br>
617 <%= submit_button 'Login' %>
618 <% end %>
619
620 @@ index.html.ep
621 % layout 'default';
622 % title 'Welcome';
623 <% if (my $message = flash 'message' ) { %>
624 <b><%= $message %></b><br>
625 <% } %>
626 Welcome <%= session 'name' %>!<br>
627 <%= link_to logout => begin %>
628 Logout
629 <% end %>
9fcb9d0 added login example to the tutorial demonstrating sessions
Sebastian Riedel authored
630
ef8a00e @kraih ho ho ho
authored
631 =head2 Secret
632
19f6f6d @kraih added file upload example to lite tutorial
authored
633 Note that you should use a custom C<secret> to make signed cookies really
634 secure.
305e6bc mention secret passphrase in tutorial
Sebastian Riedel authored
635
e8805d1 @kraih changed indentation style
authored
636 app->secret('My secret passphrase here!');
305e6bc mention secret passphrase in tutorial
Sebastian Riedel authored
637
19f6f6d @kraih added file upload example to lite tutorial
authored
638 =head2 File Uploads
639
640 All files uploaded via C<multipart/form-data> request are automatically
641 available as L<Mojo::Upload> instances.
642 And you don't have to worry about memory usage, because all files above
f825c60 @kraih fixed typo
authored
643 C<250KB> will be automatically streamed into a temporary file.
19f6f6d @kraih added file upload example to lite tutorial
authored
644
645 use Mojolicious::Lite;
646
647 any '/upload' => sub {
648 my $self = shift;
f825c60 @kraih fixed typo
authored
649 if (my $example = $self->req->upload('example')) {
650 my $size = $example->size;
651 my $name = $example->filename;
19f6f6d @kraih added file upload example to lite tutorial
authored
652 $self->render(text => "Thanks for uploading $size byte file $name.");
653 }
654 };
655
656 app->start;
657 __DATA__
658
659 @@ upload.html.ep
660 <!doctype html><html>
661 <head><title>Upload</title></head>
662 <body>
663 <%= form_for upload =>
dfccce0 @kraih Revert "improved .perltidyrc"
authored
664 (method => 'post', enctype => 'multipart/form-data') => begin %>
19f6f6d @kraih added file upload example to lite tutorial
authored
665 <%= file_field 'example' %>
666 <%= submit_button 'Upload' %>
667 <% end %>
668 </body>
669 </html>
670
671 To protect you from excessively large files there is also a global limit of
672 C<5MB> by default, which you can tweak with the C<MOJO_MAX_MESSAGE_SIZE>
673 environment variable.
674
675 # Increase limit to 1GB
676 $ENV{MOJO_MAX_MESSAGE_SIZE} = 1073741824;
677
6af6d35 @kraih Deprecated Mojo::Client in favor of the much sleeker Mojo::UserAgent
authored
678 =head2 User Agent
ef8a00e @kraih ho ho ho
authored
679
4928309 @kraih added more links to lite tutorial
authored
680 With L<Mojo::UserAgent> there's a full featured HTTP 1.1 and WebSocket user
681 agent built right in.
83d6678 mention Mojo::DOM in tutorial
Sebastian Riedel authored
682 Especially in combination with L<Mojo::JSON> and L<Mojo::DOM> this can be a
683 very powerful tool.
be2bc55 pod updates
Sebastian Riedel authored
684
e8805d1 @kraih changed indentation style
authored
685 get '/test' => sub {
686 my $self = shift;
6af6d35 @kraih Deprecated Mojo::Client in favor of the much sleeker Mojo::UserAgent
authored
687 $self->render(data => $self->ua->get('http://mojolicio.us')->res->body);
e8805d1 @kraih changed indentation style
authored
688 };
be2bc55 pod updates
Sebastian Riedel authored
689
ef8a00e @kraih ho ho ho
authored
690 =head2 WebSockets
691
be2bc55 pod updates
Sebastian Riedel authored
692 WebSocket applications have never been this easy before.
693
e8805d1 @kraih changed indentation style
authored
694 websocket '/echo' => sub {
695 my $self = shift;
696 $self->on_message(sub {
697 my ($self, $message) = @_;
698 $self->send_message("echo: $message");
699 });
700 };
be2bc55 pod updates
Sebastian Riedel authored
701
ef8a00e @kraih ho ho ho
authored
702 =head2 External Templates
703
124ea7e reformatted lite examples to a tutorial
sri authored
704 External templates will be searched by the renderer in a C<templates>
705 directory.
706
e8805d1 @kraih changed indentation style
authored
707 # /external
708 any '/external' => sub {
709 my $self = shift;
ad55fb5 updated pod
Sebastian Riedel authored
710
e8805d1 @kraih changed indentation style
authored
711 # templates/foo/bar.html.ep
712 $self->render('foo/bar');
713 };
23161b3 added more lite examples
sri authored
714
ef8a00e @kraih ho ho ho
authored
715 =head2 Static Files
716
19a186e added experimental base64 support for static files in the DATA section
Sebastian Riedel authored
717 Static files will be automatically served from the C<DATA> section
718 (even Base 64 encoded) or a C<public> directory if it exists.
82dfd94 protect inline templates from static dispatcher
Sebastian Riedel authored
719
e8805d1 @kraih changed indentation style
authored
720 @@ something.js
721 alert('hello!');
124ea7e reformatted lite examples to a tutorial
sri authored
722
e8805d1 @kraih changed indentation style
authored
723 @@ test.txt (base64)
724 dGVzdCAxMjMKbGFsYWxh
19a186e added experimental base64 support for static files in the DATA section
Sebastian Riedel authored
725
951cb5a @kraih documentation tweaks
authored
726 $ mkdir public
727 $ mv something.js public/something.js
23161b3 added more lite examples
sri authored
728
ef8a00e @kraih ho ho ho
authored
729 =head2 Testing
730
a760263 added lite testing example
Sebastian Riedel authored
731 Testing your application is as easy as creating a C<t> directory and filling
a821f78 added external lite app test
Sebastian Riedel authored
732 it with normal Perl unit tests.
a760263 added lite testing example
Sebastian Riedel authored
733
e8805d1 @kraih changed indentation style
authored
734 use Test::More tests => 3;
735 use Test::Mojo;
a760263 added lite testing example
Sebastian Riedel authored
736
e8805d1 @kraih changed indentation style
authored
737 use FindBin;
6e71227 @kraih updated tests and documentation for the latest home directory detection ...
authored
738 require "$FindBin::Bin/../myapp.pl";
a760263 added lite testing example
Sebastian Riedel authored
739
e8805d1 @kraih changed indentation style
authored
740 my $t = Test::Mojo->new;
741 $t->get_ok('/')->status_is(200)->content_like(qr/Funky!/);
a760263 added lite testing example
Sebastian Riedel authored
742
743 Run all unit tests with the C<test> command.
744
951cb5a @kraih documentation tweaks
authored
745 $ ./myapp.pl test
a760263 added lite testing example
Sebastian Riedel authored
746
9e52f8e default to silent tests in Test::Mojo
Sebastian Riedel authored
747 To make your tests more noisy and show you all log messages you can also
748 change the application log level directly in your test files.
0acf934 pod updates
Sebastian Riedel authored
749
e8805d1 @kraih changed indentation style
authored
750 $t->app->log->level('debug');
0acf934 pod updates
Sebastian Riedel authored
751
ef8a00e @kraih ho ho ho
authored
752 =head2 Mode
753
124ea7e reformatted lite examples to a tutorial
sri authored
754 To disable debug messages later in a production setup you can change the
755 L<Mojolicious> mode, default will be C<development>.
756
951cb5a @kraih documentation tweaks
authored
757 $ ./myapp.pl --mode production
f3acdb1 added more lite examples
sri authored
758
ef8a00e @kraih ho ho ho
authored
759 =head2 Logging
760
2cf59ac @kraih fixed typo
authored
761 L<Mojo::Log> messages will be automatically written to a C<log/$mode.log>
762 file if a C<log> directory exists.
124ea7e reformatted lite examples to a tutorial
sri authored
763
951cb5a @kraih documentation tweaks
authored
764 $ mkdir log
5e23803 fixed typo
sri authored
765
124ea7e reformatted lite examples to a tutorial
sri authored
766 For more control the L<Mojolicious> instance can be accessed directly.
767
e8805d1 @kraih changed indentation style
authored
768 app->log->level('error');
769 app->routes->route('/foo/:bar')->via('get')->to(cb => sub {
770 my $self = shift;
4928309 @kraih added more links to lite tutorial
authored
771 $self->app->log->debug('Got a request for "Hello Mojo!".');
e8805d1 @kraih changed indentation style
authored
772 $self->render(text => 'Hello Mojo!');
773 });
885a5e8 added more lite examples
sri authored
774
ef8a00e @kraih ho ho ho
authored
775 =head2 Growing
776
928931f @kraih fixed a few typos
authored
777 In case a lite app needs to grow, lite and real L<Mojolicious> applications
124ea7e reformatted lite examples to a tutorial
sri authored
778 can be easily mixed to make the transition process very smooth.
779
e8805d1 @kraih changed indentation style
authored
780 package MyApp::Foo;
781 use Mojo::Base 'Mojolicious::Controller';
124ea7e reformatted lite examples to a tutorial
sri authored
782
e8805d1 @kraih changed indentation style
authored
783 sub index { shift->render(text => 'It works!') }
124ea7e reformatted lite examples to a tutorial
sri authored
784
e8805d1 @kraih changed indentation style
authored
785 package main;
786 use Mojolicious::Lite;
124ea7e reformatted lite examples to a tutorial
sri authored
787
e8805d1 @kraih changed indentation style
authored
788 get '/bar' => sub { shift->render(text => 'This too!') };
124ea7e reformatted lite examples to a tutorial
sri authored
789
e8805d1 @kraih changed indentation style
authored
790 app->routes->namespace('MyApp');
791 app->routes->route('/foo/:action')->via('get')->to('foo#index');
a146626 added Mojolicious::Lite
sri authored
792
e8805d1 @kraih changed indentation style
authored
793 app->start;
a146626 added Mojolicious::Lite
sri authored
794
c43396b updated tutorial
Sebastian Riedel authored
795 There is also a helper command to generate a full L<Mojolicious> example that
3e366a9 updated tutorial
Sebastian Riedel authored
796 will let you explore the astonishing similarities between
797 L<Mojolicious::Lite> and L<Mojolicious> applications.
798 Both share about 99% of the same code, so almost everything you learned in
799 this tutorial applies there too. :)
c43396b updated tutorial
Sebastian Riedel authored
800
951cb5a @kraih documentation tweaks
authored
801 $ mojo generate app
c43396b updated tutorial
Sebastian Riedel authored
802
c6cf6a8 @kraih pod improvements
authored
803 =head2 More
804
805 You can continue with L<Mojolicious::Guides> now, and don't forget to have
f0f0cff @kraih link to more documentation
authored
806 fun!
c43396b updated tutorial
Sebastian Riedel authored
807
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
808 =head1 FUNCTIONS
809
810 L<Mojolicious::Lite> implements the following functions.
811
812 =head2 C<any>
813
e8805d1 @kraih changed indentation style
authored
814 my $route = any '/:foo' => sub {...};
815 my $route = any [qw/get post/] => '/:foo' => sub {...};
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
816
817 Generate route matching any of the listed HTTP request methods or all.
818 See also the tutorial above for more argument variations.
819
820 =head2 C<app>
821
e8805d1 @kraih changed indentation style
authored
822 my $app = app;
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
823
824 The L<Mojolicious::Lite> application.
825
998570d @kraih added put and del functions to Mojolicious::Lite
authored
826 =head2 C<del>
827
e8805d1 @kraih changed indentation style
authored
828 my $route = del '/:foo' => sub {...};
998570d @kraih added put and del functions to Mojolicious::Lite
authored
829
830 Generate route matching only C<DELETE> requests.
831 See also the tutorial above for more argument variations.
832
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
833 =head2 C<get>
834
e8805d1 @kraih changed indentation style
authored
835 my $route = get '/:foo' => sub {...};
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
836
837 Generate route matching only C<GET> requests.
838 See also the tutorial above for more argument variations.
839
9deddc7 @kraih added experimental helper function to Mojolicious::Lite
authored
840 =head2 C<helper>
841
842 helper foo => sub {...};
843
e12b69a @kraih updated changes and documentation
authored
844 Alias for L<Mojolicious/"helper">.
9deddc7 @kraih added experimental helper function to Mojolicious::Lite
authored
845 Note that this function is EXPERIMENTAL and might change without warning!
846
8b652a3 @kraih added experimental hook function to Mojolicious::Lite
authored
847 =head2 C<hook>
848
849 hook after_dispatch => sub {...};
850
e12b69a @kraih updated changes and documentation
authored
851 Alias for L<Mojolicious/"hook">.
8b652a3 @kraih added experimental hook function to Mojolicious::Lite
authored
852 Note that this function is EXPERIMENTAL and might change without warning!
853
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
854 =head2 C<plugin>
855
84d75f3 @kraih support and encourage camel case plugin names everywhere
authored
856 plugin 'SomeThing';
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
857
e12b69a @kraih updated changes and documentation
authored
858 Alias for L<Mojolicious/"plugin">.
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
859
860 =head2 C<post>
861
e8805d1 @kraih changed indentation style
authored
862 my $route = post '/:foo' => sub {...};
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
863
864 Generate route matching only C<POST> requests.
865 See also the tutorial above for more argument variations.
866
998570d @kraih added put and del functions to Mojolicious::Lite
authored
867 =head2 C<put>
868
e8805d1 @kraih changed indentation style
authored
869 my $route = put '/:foo' => sub {...};
998570d @kraih added put and del functions to Mojolicious::Lite
authored
870
871 Generate route matching only C<PUT> requests.
872 See also the tutorial above for more argument variations.
873
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
874 =head2 C<under>
875
e8805d1 @kraih changed indentation style
authored
876 my $route = under sub {...};
877 my $route = under '/:foo';
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
878
879 Generate bridge to which all following routes are automatically appended.
880 See also the tutorial above for more argument variations.
881
882 =head2 C<websocket>
883
e8805d1 @kraih changed indentation style
authored
884 my $route = websocket '/:foo' => sub {...};
79a24c9 list functions in Mojolicious::Lite
Sebastian Riedel authored
885
886 Generate route matching only C<WebSocket> handshakes.
887 See also the tutorial above for more argument variations.
888
a146626 added Mojolicious::Lite
sri authored
889 =head1 ATTRIBUTES
890
891 L<Mojolicious::Lite> inherits all attributes from L<Mojolicious>.
892
893 =head1 METHODS
894
fcf3ef7 added the ability to have multiple Mojolicious::Lite apps at once (Mojol...
Sebastian Riedel authored
895 L<Mojolicious::Lite> inherits all methods from L<Mojolicious>.
a146626 added Mojolicious::Lite
sri authored
896
d49a4ee pod cleanup
Sebastian Riedel authored
897 =head1 SEE ALSO
898
eb527e6 @kraih christmas preparations
authored
899 L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
d49a4ee pod cleanup
Sebastian Riedel authored
900
a146626 added Mojolicious::Lite
sri authored
901 =cut
Something went wrong with that request. Please try again.