Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 576 lines (409 sloc) 13.502 kb
9ebb132 cleanup
Sebastian Riedel authored
1 # Copyright (C) 2008-2010, Sebastian Riedel.
a146626 added Mojolicious::Lite
sri authored
2
3 package Mojolicious::Lite;
4
5 use strict;
6 use warnings;
7
8 use base 'Mojolicious';
9
10 use File::Spec;
11 use FindBin;
12
c69ff10 MOJO_RELOAD=1 now works with Mojolicious::Lite, have fun!
Sebastian Riedel authored
13 # Make reloading work
14 BEGIN { $INC{$0} = $0 }
15
a146626 added Mojolicious::Lite
sri authored
16 # It's the future, my parents, my co-workers, my girlfriend,
17 # I'll never see any of them ever again... YAHOOO!
18 sub import {
19 my $class = shift;
20
56f0c8b lite apps are strict by default
sri authored
21 # Lite apps are strict!
22 strict->import;
23 warnings->import;
24
a146626 added Mojolicious::Lite
sri authored
25 # Home
fd7db72 fixed home handling in Mojolicious::Lite
sri authored
26 $ENV{MOJO_HOME} ||= File::Spec->catdir(split '/', $FindBin::Bin);
a146626 added Mojolicious::Lite
sri authored
27
28 # Initialize app
fcf3ef7 added the ability to have multiple Mojolicious::Lite apps at once (Mojol...
Sebastian Riedel authored
29 my $app = $class->new;
a146626 added Mojolicious::Lite
sri authored
30
d8fb4f3 added ladders to Mojolicious::Lite, they are like bridges but lite
Sebastian Riedel authored
31 # Initialize routes
fcf3ef7 added the ability to have multiple Mojolicious::Lite apps at once (Mojol...
Sebastian Riedel authored
32 my $routes = $app->routes;
d8fb4f3 added ladders to Mojolicious::Lite, they are like bridges but lite
Sebastian Riedel authored
33
a146626 added Mojolicious::Lite
sri authored
34 # Route generator
35 my $route = sub {
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
36 my ($methods, @args) = @_;
a146626 added Mojolicious::Lite
sri authored
37
38 my ($cb, $constraints, $defaults, $name, $pattern);
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
39 my $conditions = [];
a146626 added Mojolicious::Lite
sri authored
40
41 # Route information
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
42 my $condition;
43 while (my $arg = shift @args) {
44
45 # Condition can be everything
46 if ($condition) {
47 push @$conditions, $condition => $arg;
48 $condition = undef;
49 }
a146626 added Mojolicious::Lite
sri authored
50
51 # First scalar is the pattern
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
52 elsif (!ref $arg && !$pattern) { $pattern = $arg }
53
54 # Scalar
55 elsif (!ref $arg && @args) { $condition = $arg }
a146626 added Mojolicious::Lite
sri authored
56
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
57 # Last scalar is the route name
a146626 added Mojolicious::Lite
sri authored
58 elsif (!ref $arg) { $name = $arg }
59
228acdd added any support and many more tests to Mojolicious::Lite
sri authored
60 # Callback
a146626 added Mojolicious::Lite
sri authored
61 elsif (ref $arg eq 'CODE') { $cb = $arg }
62
0b20c1c constraints are now an arrayref
sri authored
63 # Constraints
64 elsif (ref $arg eq 'ARRAY') { $constraints = $arg }
a146626 added Mojolicious::Lite
sri authored
65
0b20c1c constraints are now an arrayref
sri authored
66 # Defaults
a146626 added Mojolicious::Lite
sri authored
67 elsif (ref $arg eq 'HASH') { $defaults = $arg }
68 }
69
70 # Defaults
71 $cb ||= sub {1};
0b20c1c constraints are now an arrayref
sri authored
72 $constraints ||= [];
a146626 added Mojolicious::Lite
sri authored
73
74 # Merge
75 $defaults ||= {};
76 $defaults = {%$defaults, callback => $cb};
77
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
78 # Name
79 $name ||= '';
80
d8fb4f3 added ladders to Mojolicious::Lite, they are like bridges but lite
Sebastian Riedel authored
81 # Create bridge
fcf3ef7 added the ability to have multiple Mojolicious::Lite apps at once (Mojol...
Sebastian Riedel authored
82 return $routes =
83 $app->routes->bridge($pattern, {@$constraints})->over($conditions)
d8fb4f3 added ladders to Mojolicious::Lite, they are like bridges but lite
Sebastian Riedel authored
84 ->to($defaults)->name($name)
85 if !ref $methods && $methods eq 'ladder';
86
7ee211b added WebSocket support to Mojolicious and Mojolicious::Lite
Sebastian Riedel authored
87 # WebSocket?
88 my $websocket = 1 if !ref $methods && $methods eq 'websocket';
89 $methods = [] if $websocket;
90
a146626 added Mojolicious::Lite
sri authored
91 # Create route
7ee211b added WebSocket support to Mojolicious and Mojolicious::Lite
Sebastian Riedel authored
92 my $route =
93 $routes->route($pattern, {@$constraints})->over($conditions)
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
94 ->via($methods)->to($defaults)->name($name);
7ee211b added WebSocket support to Mojolicious and Mojolicious::Lite
Sebastian Riedel authored
95
96 # WebSocket
97 $route->websocket if $websocket;
98
99 return $route;
a146626 added Mojolicious::Lite
sri authored
100 };
101
102 # Prepare exports
103 my $caller = caller;
104 no strict 'refs';
c69ff10 MOJO_RELOAD=1 now works with Mojolicious::Lite, have fun!
Sebastian Riedel authored
105 no warnings 'redefine';
a146626 added Mojolicious::Lite
sri authored
106
a0bdede fixed typo
Sebastian Riedel authored
107 # Default template class
108 $app->renderer->default_template_class($caller);
109
a146626 added Mojolicious::Lite
sri authored
110 # Export
7ee211b added WebSocket support to Mojolicious and Mojolicious::Lite
Sebastian Riedel authored
111 *{"${caller}::app"} = sub {$app};
112 *{"${caller}::any"} = sub { $route->(ref $_[0] ? shift : [], @_) };
113 *{"${caller}::get"} = sub { $route->('get', @_) };
114 *{"${caller}::ladder"} = sub { $route->('ladder', @_) };
115 *{"${caller}::plugin"} = sub { $app->plugin(@_) };
116 *{"${caller}::post"} = sub { $route->('post', @_) };
117 *{"${caller}::websocket"} = sub { $route->('websocket', @_) };
a146626 added Mojolicious::Lite
sri authored
118
fcf3ef7 added the ability to have multiple Mojolicious::Lite apps at once (Mojol...
Sebastian Riedel authored
119 # We are most likely the app in a lite environment
c69ff10 MOJO_RELOAD=1 now works with Mojolicious::Lite, have fun!
Sebastian Riedel authored
120 $ENV{MOJO_APP} = $app;
a146626 added Mojolicious::Lite
sri authored
121
fcf3ef7 added the ability to have multiple Mojolicious::Lite apps at once (Mojol...
Sebastian Riedel authored
122 # Shagadelic!
123 *{"${caller}::shagadelic"} = sub { Mojolicious::Lite->start(@_) };
a146626 added Mojolicious::Lite
sri authored
124 }
125
126 1;
127 __END__
128
129 =head1 NAME
130
131 Mojolicious::Lite - Micro Web Framework
132
133 =head1 SYNOPSIS
134
24b3bff added ->render(text => "") support and many lite examples
sri authored
135 # Using Mojolicious::Lite will enable "strict" and "warnings"
a146626 added Mojolicious::Lite
sri authored
136 use Mojolicious::Lite;
137
9b87480 improved tutorial a bit
sri authored
138 # Route with placeholder
124ea7e reformatted lite examples to a tutorial
sri authored
139 get '/:foo' => sub {
a146626 added Mojolicious::Lite
sri authored
140 my $self = shift;
4069892 added render_text method
Sebastian Riedel authored
141 $self->render_text('Yea baby!');
a146626 added Mojolicious::Lite
sri authored
142 };
143
9c69cc4 renamed script system to command system
Sebastian Riedel authored
144 # Start the Mojolicious command system
a146626 added Mojolicious::Lite
sri authored
145 shagadelic;
146
124ea7e reformatted lite examples to a tutorial
sri authored
147 =head1 DESCRIPTION
148
149 L<Mojolicous::Lite> is a micro web framework built around L<Mojolicious>.
150
9b87480 improved tutorial a bit
sri authored
151 A minimal application looks like this.
152
153 #!/usr/bin/env perl
154
155 use Mojolicious::Lite;
156
157 get '/' => sub {
158 my $self = shift;
4069892 added render_text method
Sebastian Riedel authored
159 $self->render_text('Yea baby!');
9b87480 improved tutorial a bit
sri authored
160 };
161
162 shagadelic;
163
9c69cc4 renamed script system to command system
Sebastian Riedel authored
164 There is also a helper command to generate a small example application.
124ea7e reformatted lite examples to a tutorial
sri authored
165
166 % mojolicious generate lite_app
167
9c69cc4 renamed script system to command system
Sebastian Riedel authored
168 All the normal L<Mojolicious> command options are available from the command
758c94a fixed typo
sri authored
169 line.
124ea7e reformatted lite examples to a tutorial
sri authored
170
24b3bff added ->render(text => "") support and many lite examples
sri authored
171 % ./myapp.pl daemon
172 Server available at http://127.0.0.1:3000.
124ea7e reformatted lite examples to a tutorial
sri authored
173
9ac9059 added more lite examples
sri authored
174 % ./myapp.pl daemon 8080
175 Server available at http://127.0.0.1:8080.
124ea7e reformatted lite examples to a tutorial
sri authored
176
0112933 cleaned up the whole script system
sri authored
177 % ./myapp.pl daemon_prefork
9ac9059 added more lite examples
sri authored
178 Server available at http://127.0.0.1:3000.
124ea7e reformatted lite examples to a tutorial
sri authored
179
0112933 cleaned up the whole script system
sri authored
180 % ./myapp.pl cgi
24b3bff added ->render(text => "") support and many lite examples
sri authored
181 ...CGI output...
124ea7e reformatted lite examples to a tutorial
sri authored
182
0112933 cleaned up the whole script system
sri authored
183 % ./myapp.pl fastcgi
24b3bff added ->render(text => "") support and many lite examples
sri authored
184 ...Blocking FastCGI main loop...
185
9c69cc4 renamed script system to command system
Sebastian Riedel authored
186 The shagadelic call that starts the L<Mojolicious> command system can be
124ea7e reformatted lite examples to a tutorial
sri authored
187 customized to override normal C<@ARGV> use.
188
0112933 cleaned up the whole script system
sri authored
189 shagadelic('cgi');
24b3bff added ->render(text => "") support and many lite examples
sri authored
190
8a62cf3 updated lite tutorial with --reload option
Sebastian Riedel authored
191 Your application will automatically reload itself if you set the C<--reload>
192 option, so you don't have to restart the server after every change.
2e9c57b added MOJO_RELOAD section to Mojolicious::Lite tutorial
Sebastian Riedel authored
193
8a62cf3 updated lite tutorial with --reload option
Sebastian Riedel authored
194 % ./myapp.pl daemon --reload
195 Server available at http://127.0.0.1:3000.
2e9c57b added MOJO_RELOAD section to Mojolicious::Lite tutorial
Sebastian Riedel authored
196
c564032 fixed typo
sri authored
197 Routes are basically just fancy paths that can contain different kinds of
124ea7e reformatted lite examples to a tutorial
sri authored
198 placeholders.
199
200 # /foo
c564032 fixed typo
sri authored
201 get '/foo' => sub {
0b889b9 added more examples
sri authored
202 my $self = shift;
4069892 added render_text method
Sebastian Riedel authored
203 $self->render_text('Yea baby!');
0b889b9 added more examples
sri authored
204 };
205
124ea7e reformatted lite examples to a tutorial
sri authored
206 All routes can have a name associated with them, this allows automatic
207 template detection and back referencing with C<url_for>.
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
208 Names are always the last argument.
124ea7e reformatted lite examples to a tutorial
sri authored
209
210 # /
211 get '/' => 'index';
212
213 # /foo
214 get '/foo' => 'foo';
215
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
216 # /bar
217 get '/bar' => sub {
218 my $self = shift;
219 $self->render_text('Hi!')
220 } => 'bar';
221
24b3bff added ->render(text => "") support and many lite examples
sri authored
222 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
223
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
224 @@ index.html.ep
225 <a href="<%= url_for 'foo' %>">Foo</a>.
226 <a href="<%= url_for 'bar' %>">Bar</a>.
124ea7e reformatted lite examples to a tutorial
sri authored
227
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
228 @@ foo.html.ep
229 <a href="<%= url_for 'index' %>">Home</a>.
9ac9059 added more lite examples
sri authored
230
124ea7e reformatted lite examples to a tutorial
sri authored
231 Templates can have layouts.
232
233 # GET /with_layout
885a5e8 added more lite examples
sri authored
234 get '/with_layout' => sub {
235 my $self = shift;
c8d4f8f updated pod
Sebastian Riedel authored
236 $self->render('with_layout', layout => 'green');
885a5e8 added more lite examples
sri authored
237 };
124ea7e reformatted lite examples to a tutorial
sri authored
238
885a5e8 added more lite examples
sri authored
239 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
240
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
241 @@ with_layout.html.ep
885a5e8 added more lite examples
sri authored
242 We've got content!
124ea7e reformatted lite examples to a tutorial
sri authored
243
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
244 @@ layouts/green.html.ep
93575c8 fixed all html5 tags
Sebastian Riedel authored
245 <!doctype html><html>
97d759b cleanup
Sebastian Riedel authored
246 <head><title>Green!</title></head>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
247 <body><%= content %></body>
248 </html>
249
250 Templates can also extend each other.
251
252 # GET /
253 get '/' => 'first';
254
2aa74f8 updated example
Sebastian Riedel authored
255 # GET /second
256 get '/second' => 'second';
257
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
258 __DATA__
259
260 @@ first.html.ep
261 % extends 'second';
da22f33 updated example
Sebastian Riedel authored
262 %{ content header =>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
263 <title>Howdy!</title>
264 %}
6e0e78b updated example
Sebastian Riedel authored
265 First!
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
266
267 @@ second.html.ep
268 % layout 'third';
da22f33 updated example
Sebastian Riedel authored
269 %{ content header =>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
270 <title>Welcome!</title>
271 %}
6e0e78b updated example
Sebastian Riedel authored
272 Second!
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
273
274 @@ layouts/third.html.ep
275 <!doctype html><html>
276 <head>
277 <%{= content header => %>
91f8058 updated example
Sebastian Riedel authored
278 <title>Lame default title...</title>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
279 <%}%>
280 </head>
281 <body><%= content %></body>
885a5e8 added more lite examples
sri authored
282 </html>
283
124ea7e reformatted lite examples to a tutorial
sri authored
284 Route placeholders allow capturing parts of a request path until a C</> or
5de8c36 added routes captures to params in Mojolicious
Sebastian Riedel authored
285 C<.> separator occurs, results will be stored by name in the C<stash> and
286 C<param>.
124ea7e reformatted lite examples to a tutorial
sri authored
287
288 # /foo/*
289 get '/foo/:bar' => sub {
f90f056 fixed typo
sri authored
290 my $self = shift;
291 my $bar = $self->stash('bar');
4069892 added render_text method
Sebastian Riedel authored
292 $self->render_text("Our :bar placeholder matched $bar");
9ac9059 added more lite examples
sri authored
293 };
24b3bff added ->render(text => "") support and many lite examples
sri authored
294
124ea7e reformatted lite examples to a tutorial
sri authored
295 # /*something/foo
296 get '/(:bar)something/foo' => sub {
f90f056 fixed typo
sri authored
297 my $self = shift;
5de8c36 added routes captures to params in Mojolicious
Sebastian Riedel authored
298 my $bar = $self->param('bar');
4069892 added render_text method
Sebastian Riedel authored
299 $self->render_text("Our :bar placeholder matched $bar");
24b3bff added ->render(text => "") support and many lite examples
sri authored
300 };
301
124ea7e reformatted lite examples to a tutorial
sri authored
302 Relaxed placeholders allow matching of everything until a C</> occurs.
303
304 # GET /hello/*
885a5e8 added more lite examples
sri authored
305 get '/hello/(.you)' => sub {
c8d4f8f updated pod
Sebastian Riedel authored
306 shift->render('groovy');
885a5e8 added more lite examples
sri authored
307 };
124ea7e reformatted lite examples to a tutorial
sri authored
308
885a5e8 added more lite examples
sri authored
309 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
310
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
311 @@ groovy.html.ep
312 Your name is <%= $you %>.
885a5e8 added more lite examples
sri authored
313
124ea7e reformatted lite examples to a tutorial
sri authored
314 Wildcard placeholders allow matching absolutely everything, including
315 C</> and C<.>.
316
317 # /hello/*
885a5e8 added more lite examples
sri authored
318 get '/hello/(*you)' => sub {
c8d4f8f updated pod
Sebastian Riedel authored
319 shift->render('groovy');
885a5e8 added more lite examples
sri authored
320 };
19499ca fixed typo
sri authored
321
885a5e8 added more lite examples
sri authored
322 __DATA__
19499ca fixed typo
sri authored
323
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
324 @@ groovy.html.ep
325 Your name is <%= $you %>.
885a5e8 added more lite examples
sri authored
326
124ea7e reformatted lite examples to a tutorial
sri authored
327 Routes can be restricted to specific request methods.
328
329 # GET /bye
4069892 added render_text method
Sebastian Riedel authored
330 get '/bye' => sub { shift->render_text('Bye!') };
124ea7e reformatted lite examples to a tutorial
sri authored
331
332 # POST /bye
4069892 added render_text method
Sebastian Riedel authored
333 post '/bye' => sub { shift->render_text('Bye!') };
124ea7e reformatted lite examples to a tutorial
sri authored
334
335 # GET|POST|DELETE /bye
336 any [qw/get post delete/] => '/bye' => sub {
4069892 added render_text method
Sebastian Riedel authored
337 shift->render_text('Bye!');
124ea7e reformatted lite examples to a tutorial
sri authored
338 };
339
340 # /baz
341 any '/baz' => sub {
1ff4e12 fixed typo
sri authored
342 my $self = shift;
124ea7e reformatted lite examples to a tutorial
sri authored
343 my $method = $self->req->method;
4069892 added render_text method
Sebastian Riedel authored
344 $self->render_text("You called /baz with $method");
124ea7e reformatted lite examples to a tutorial
sri authored
345 };
346
347 All placeholders get compiled to a regex internally, with regex constraints
348 this process can be easily customized.
349
937a113 fixed typo
sri authored
350 # /*
1ff4e12 fixed typo
sri authored
351 any '/:bar' => [bar => qr/\d+/] => sub {
24b3bff added ->render(text => "") support and many lite examples
sri authored
352 my $self = shift;
5de8c36 added routes captures to params in Mojolicious
Sebastian Riedel authored
353 my $bar = $self->param('bar');
4069892 added render_text method
Sebastian Riedel authored
354 $self->render_text("Our :bar placeholder matched $bar");
24b3bff added ->render(text => "") support and many lite examples
sri authored
355 };
356
124ea7e reformatted lite examples to a tutorial
sri authored
357 Routes allow default values to make placeholders optional.
358
359 # /hello/*
24b3bff added ->render(text => "") support and many lite examples
sri authored
360 get '/hello/:name' => {name => 'Sebastian'} => sub {
361 my $self = shift;
c8d4f8f updated pod
Sebastian Riedel authored
362 $self->render('groovy', format => 'txt');
24b3bff added ->render(text => "") support and many lite examples
sri authored
363 };
124ea7e reformatted lite examples to a tutorial
sri authored
364
24b3bff added ->render(text => "") support and many lite examples
sri authored
365 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
366
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
367 @@ groovy.txt.ep
368 My name is <%= $name %>.
24b3bff added ->render(text => "") support and many lite examples
sri authored
369
124ea7e reformatted lite examples to a tutorial
sri authored
370 All those features can be easily used together.
24b3bff added ->render(text => "") support and many lite examples
sri authored
371
124ea7e reformatted lite examples to a tutorial
sri authored
372 # /everything/*?name=*
24b3bff added ->render(text => "") support and many lite examples
sri authored
373 get '/everything/:stuff' => [stuff => qr/\d+/] => {stuff => 23} => sub {
c8d4f8f updated pod
Sebastian Riedel authored
374 shift->render('welcome');
24b3bff added ->render(text => "") support and many lite examples
sri authored
375 };
124ea7e reformatted lite examples to a tutorial
sri authored
376
24b3bff added ->render(text => "") support and many lite examples
sri authored
377 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
378
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
379 @@ welcome.html.ep
380 Stuff is <%= $stuff %>.
381 Query param name is <%= param 'name' %>.
24b3bff added ->render(text => "") support and many lite examples
sri authored
382
124ea7e reformatted lite examples to a tutorial
sri authored
383 Here's a fully functional example for a html form handling application using
384 multiple features at once.
385
386 #!/usr/bin/env perl
387
388 use Mojolicious::Lite;
389
390 get '/' => 'index';
391
eee70d7 fixed typo in tutorial
Sebastian Riedel authored
392 post '/form' => sub {
124ea7e reformatted lite examples to a tutorial
sri authored
393 my $self = shift;
394
b26e6b6 added param to Mojolicious::Controller
Sebastian Riedel authored
395 my $groovy = $self->param('groovy') || 'Austin Powers';
124ea7e reformatted lite examples to a tutorial
sri authored
396 $groovy =~ s/[^\w\s]+//g;
397
398 $self->render(
399 template => 'welcome',
400 layout => 'funky',
401 groovy => $groovy
402 );
eee70d7 fixed typo in tutorial
Sebastian Riedel authored
403 } => 'form';
124ea7e reformatted lite examples to a tutorial
sri authored
404
405 shagadelic;
406 __DATA__
407
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
408 @@ index.html.ep
409 % layout 'funky');
124ea7e reformatted lite examples to a tutorial
sri authored
410 Who is groovy?
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
411 <form action="<%= url_for 'form' %>" method="POST">
124ea7e reformatted lite examples to a tutorial
sri authored
412 <input type="text" name="groovy" />
ca088e1 fixed typo
sri authored
413 <input type="submit" value="Woosh!">
124ea7e reformatted lite examples to a tutorial
sri authored
414 </form>
415
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
416 @@ welcome.html.ep
417 <%= $groovy %> is groovy!
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
418 <%= include 'menu' %>
124ea7e reformatted lite examples to a tutorial
sri authored
419
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
420 @@ menu.html.ep
421 <a href="<%= url_for 'index' %>">Try again</a>
124ea7e reformatted lite examples to a tutorial
sri authored
422
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
423 @@ layouts/funky.html.ep
93575c8 fixed all html5 tags
Sebastian Riedel authored
424 <!doctype html><html>
97d759b cleanup
Sebastian Riedel authored
425 <head><title>Funky!</title></head>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
426 <body><%= content %>
97d759b cleanup
Sebastian Riedel authored
427 </body>
124ea7e reformatted lite examples to a tutorial
sri authored
428 </html>
429
e6502f7 cleaned up tutorial
Sebastian Riedel authored
430 Ladders can be used for authentication and to share code between multiple
431 routes.
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
432 All routes following a ladder are only evaluated if the ladder returns a
433 true value.
434
435 use Mojolicious::Lite;
436
437 # Authenticate based on name parameter
438 ladder sub {
439 my $self = shift;
440
441 # Authenticated
442 my $name = $self->param('name') || '';
443 return 1 if $name eq 'Bender';
444
445 # Not authenticated
446 $self->render('denied');
447 return;
448 };
449
450 # GET / (with ladder authentication)
451 get '/' => 'index';
452
453 shagadelic;
454 __DATA__;
455
456 @@ denied.html.ep
457 You are not Bender, permission denied!
458
459 @@ index.html.ep
460 Hi Bender!
461
5d59ed2 fixed typo
Sebastian Riedel authored
462 Conditions such as C<agent> allow even more powerful route constructs.
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
463
464 # /foo
465 get '/foo' => (agent => qr/Firefox/) => sub {
466 shift->render_text('Congratulations, you are using a cool browser!');
467 }
468
469 # /foo
470 get '/foo' => (agent => qr/Internet Explorer/) => sub {
471 shift->render_text('Dude, you really need to upgrade to Firefox!');
472 }
473
124ea7e reformatted lite examples to a tutorial
sri authored
474 Formats can be automatically detected by looking at file extensions.
475
476 # /detection.html
477 # /detection.txt
24b3bff added ->render(text => "") support and many lite examples
sri authored
478 get '/detection' => sub {
479 my $self = shift;
c8d4f8f updated pod
Sebastian Riedel authored
480 $self->render('detected');
24b3bff added ->render(text => "") support and many lite examples
sri authored
481 };
124ea7e reformatted lite examples to a tutorial
sri authored
482
24b3bff added ->render(text => "") support and many lite examples
sri authored
483 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
484
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
485 @@ detected.html.ep
93575c8 fixed all html5 tags
Sebastian Riedel authored
486 <!doctype html><html>
97d759b cleanup
Sebastian Riedel authored
487 <head><title>Detected!</title></head>
488 <body>HTML was detected.</body>
24b3bff added ->render(text => "") support and many lite examples
sri authored
489 </html>
124ea7e reformatted lite examples to a tutorial
sri authored
490
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
491 @@ detected.txt.ep
24b3bff added ->render(text => "") support and many lite examples
sri authored
492 TXT was detected.
493
124ea7e reformatted lite examples to a tutorial
sri authored
494 External templates will be searched by the renderer in a C<templates>
495 directory.
496
497 # /external
23161b3 added more lite examples
sri authored
498 any '/external' => sub {
499 my $self = shift;
ad55fb5 updated pod
Sebastian Riedel authored
500
ee5d381 fixed a few cases where exceptions and not found events would result in ...
Sebastian Riedel authored
501 # templates/foo/bar.html.ep
c8d4f8f updated pod
Sebastian Riedel authored
502 $self->render('foo/bar');
23161b3 added more lite examples
sri authored
503 };
504
124ea7e reformatted lite examples to a tutorial
sri authored
505 Static files will be automatically served from the C<public> directory if it
506 exists.
507
23161b3 added more lite examples
sri authored
508 % mkdir public
509 % mv something.js public/something.js
510
a760263 added lite testing example
Sebastian Riedel authored
511 Testing your application is as easy as creating a C<t> directory and filling
590b113 cleanup
Sebastian Riedel authored
512 it with normal Perl unit tests like C<t/funky.t>.
a760263 added lite testing example
Sebastian Riedel authored
513
514 use Test::More tests => 3;
515 use Test::Mojo;
516
517 use FindBin;
518 require "$FindBin::Bin/../myapp.pl";
519
520 my $t = Test::Mojo->new;
521 $t->get_ok('/')->status_is(200)->content_like(qr/Funky!/);
522
523 Run all unit tests with the C<test> command.
524
525 % ./myapp.pl test
526
124ea7e reformatted lite examples to a tutorial
sri authored
527 To disable debug messages later in a production setup you can change the
528 L<Mojolicious> mode, default will be C<development>.
529
f3acdb1 added more lite examples
sri authored
530 % MOJO_MODE=production ./myapp.pl
531
124ea7e reformatted lite examples to a tutorial
sri authored
532 Log messages will be automatically written to a C<log/$mode.log> file if a
533 C<log> directory exists.
534
5e23803 fixed typo
sri authored
535 % mkdir log
536
124ea7e reformatted lite examples to a tutorial
sri authored
537 For more control the L<Mojolicious> instance can be accessed directly.
538
885a5e8 added more lite examples
sri authored
539 app->log->level('error');
540 app->routes->route('/foo/:bar')->via('get')->to(callback => sub {
541 my $self = shift;
4069892 added render_text method
Sebastian Riedel authored
542 $self->render_text('Hello Mojo!');
885a5e8 added more lite examples
sri authored
543 });
544
124ea7e reformatted lite examples to a tutorial
sri authored
545 In case a lite app needs to grow, lite and real L<Mojolicous> applications
546 can be easily mixed to make the transition process very smooth.
547
e491cf2 added example for mixed mojolicious and lite app
sri authored
548 package MyApp::Foo;
549 use base 'Mojolicious::Controller';
124ea7e reformatted lite examples to a tutorial
sri authored
550
4069892 added render_text method
Sebastian Riedel authored
551 sub index { shift->render_text('It works!') }
124ea7e reformatted lite examples to a tutorial
sri authored
552
e491cf2 added example for mixed mojolicious and lite app
sri authored
553 package main;
554 use Mojolicious::Lite;
124ea7e reformatted lite examples to a tutorial
sri authored
555
4069892 added render_text method
Sebastian Riedel authored
556 get '/bar' => sub { shift->render_text('This too!') };
124ea7e reformatted lite examples to a tutorial
sri authored
557
e491cf2 added example for mixed mojolicious and lite app
sri authored
558 app->routes->namespace('MyApp');
f5439a5 cleanup
Sebastian Riedel authored
559 app->routes->route('/foo/:action')->via('get')->to('foo#index');
a146626 added Mojolicious::Lite
sri authored
560
124ea7e reformatted lite examples to a tutorial
sri authored
561 shagadelic;
a146626 added Mojolicious::Lite
sri authored
562
563 =head1 ATTRIBUTES
564
565 L<Mojolicious::Lite> inherits all attributes from L<Mojolicious>.
566
567 =head1 METHODS
568
fcf3ef7 added the ability to have multiple Mojolicious::Lite apps at once (Mojol...
Sebastian Riedel authored
569 L<Mojolicious::Lite> inherits all methods from L<Mojolicious>.
a146626 added Mojolicious::Lite
sri authored
570
d49a4ee pod cleanup
Sebastian Riedel authored
571 =head1 SEE ALSO
572
573 L<Mojolicious>, L<Mojolicious::Book>, L<http://mojolicious.org>.
574
a146626 added Mojolicious::Lite
sri authored
575 =cut
Something went wrong with that request. Please try again.