Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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