Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 577 lines (410 sloc) 13.545 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 (Mo…
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 (Mo…
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 (Mo…
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
4a352d5 cleanup
Sebastian Riedel authored
87 # WebSocket
7ee211b added WebSocket support to Mojolicious and Mojolicious::Lite
Sebastian Riedel authored
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 (Mo…
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 (Mo…
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 …
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 …
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 …
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 …
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
641d5e0 pod cleanup
Sebastian Riedel authored
250 Templates can also pass around blocks of captured content and extend each
251 other.
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
252
253 # GET /
254 get '/' => 'first';
255
2aa74f8 updated example
Sebastian Riedel authored
256 # GET /second
257 get '/second' => 'second';
258
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
259 __DATA__
260
261 @@ first.html.ep
262 % extends 'second';
da22f33 updated example
Sebastian Riedel authored
263 %{ content header =>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
264 <title>Howdy!</title>
265 %}
6e0e78b updated example
Sebastian Riedel authored
266 First!
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
267
268 @@ second.html.ep
269 % layout 'third';
da22f33 updated example
Sebastian Riedel authored
270 %{ content header =>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
271 <title>Welcome!</title>
272 %}
6e0e78b updated example
Sebastian Riedel authored
273 Second!
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
274
275 @@ layouts/third.html.ep
276 <!doctype html><html>
277 <head>
278 <%{= content header => %>
91f8058 updated example
Sebastian Riedel authored
279 <title>Lame default title...</title>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
280 <%}%>
281 </head>
282 <body><%= content %></body>
885a5e8 added more lite examples
sri authored
283 </html>
284
124ea7e reformatted lite examples to a tutorial
sri authored
285 Route placeholders allow capturing parts of a request path until a C</> or
5de8c36 added routes captures to params in Mojolicious
Sebastian Riedel authored
286 C<.> separator occurs, results will be stored by name in the C<stash> and
287 C<param>.
124ea7e reformatted lite examples to a tutorial
sri authored
288
289 # /foo/*
290 get '/foo/:bar' => sub {
f90f056 fixed typo
sri authored
291 my $self = shift;
292 my $bar = $self->stash('bar');
4069892 added render_text method
Sebastian Riedel authored
293 $self->render_text("Our :bar placeholder matched $bar");
9ac9059 added more lite examples
sri authored
294 };
24b3bff added ->render(text => "") support and many lite examples
sri authored
295
124ea7e reformatted lite examples to a tutorial
sri authored
296 # /*something/foo
297 get '/(:bar)something/foo' => sub {
f90f056 fixed typo
sri authored
298 my $self = shift;
5de8c36 added routes captures to params in Mojolicious
Sebastian Riedel authored
299 my $bar = $self->param('bar');
4069892 added render_text method
Sebastian Riedel authored
300 $self->render_text("Our :bar placeholder matched $bar");
24b3bff added ->render(text => "") support and many lite examples
sri authored
301 };
302
124ea7e reformatted lite examples to a tutorial
sri authored
303 Relaxed placeholders allow matching of everything until a C</> occurs.
304
305 # GET /hello/*
885a5e8 added more lite examples
sri authored
306 get '/hello/(.you)' => sub {
c8d4f8f updated pod
Sebastian Riedel authored
307 shift->render('groovy');
885a5e8 added more lite examples
sri authored
308 };
124ea7e reformatted lite examples to a tutorial
sri authored
309
885a5e8 added more lite examples
sri authored
310 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
311
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
312 @@ groovy.html.ep
313 Your name is <%= $you %>.
885a5e8 added more lite examples
sri authored
314
124ea7e reformatted lite examples to a tutorial
sri authored
315 Wildcard placeholders allow matching absolutely everything, including
316 C</> and C<.>.
317
318 # /hello/*
885a5e8 added more lite examples
sri authored
319 get '/hello/(*you)' => sub {
c8d4f8f updated pod
Sebastian Riedel authored
320 shift->render('groovy');
885a5e8 added more lite examples
sri authored
321 };
19499ca fixed typo
sri authored
322
885a5e8 added more lite examples
sri authored
323 __DATA__
19499ca fixed typo
sri authored
324
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
325 @@ groovy.html.ep
326 Your name is <%= $you %>.
885a5e8 added more lite examples
sri authored
327
124ea7e reformatted lite examples to a tutorial
sri authored
328 Routes can be restricted to specific request methods.
329
330 # GET /bye
4069892 added render_text method
Sebastian Riedel authored
331 get '/bye' => sub { shift->render_text('Bye!') };
124ea7e reformatted lite examples to a tutorial
sri authored
332
333 # POST /bye
4069892 added render_text method
Sebastian Riedel authored
334 post '/bye' => sub { shift->render_text('Bye!') };
124ea7e reformatted lite examples to a tutorial
sri authored
335
336 # GET|POST|DELETE /bye
337 any [qw/get post delete/] => '/bye' => sub {
4069892 added render_text method
Sebastian Riedel authored
338 shift->render_text('Bye!');
124ea7e reformatted lite examples to a tutorial
sri authored
339 };
340
341 # /baz
342 any '/baz' => sub {
1ff4e12 fixed typo
sri authored
343 my $self = shift;
124ea7e reformatted lite examples to a tutorial
sri authored
344 my $method = $self->req->method;
4069892 added render_text method
Sebastian Riedel authored
345 $self->render_text("You called /baz with $method");
124ea7e reformatted lite examples to a tutorial
sri authored
346 };
347
348 All placeholders get compiled to a regex internally, with regex constraints
349 this process can be easily customized.
350
937a113 fixed typo
sri authored
351 # /*
1ff4e12 fixed typo
sri authored
352 any '/:bar' => [bar => qr/\d+/] => sub {
24b3bff added ->render(text => "") support and many lite examples
sri authored
353 my $self = shift;
5de8c36 added routes captures to params in Mojolicious
Sebastian Riedel authored
354 my $bar = $self->param('bar');
4069892 added render_text method
Sebastian Riedel authored
355 $self->render_text("Our :bar placeholder matched $bar");
24b3bff added ->render(text => "") support and many lite examples
sri authored
356 };
357
124ea7e reformatted lite examples to a tutorial
sri authored
358 Routes allow default values to make placeholders optional.
359
360 # /hello/*
24b3bff added ->render(text => "") support and many lite examples
sri authored
361 get '/hello/:name' => {name => 'Sebastian'} => sub {
362 my $self = shift;
c8d4f8f updated pod
Sebastian Riedel authored
363 $self->render('groovy', format => 'txt');
24b3bff added ->render(text => "") support and many lite examples
sri authored
364 };
124ea7e reformatted lite examples to a tutorial
sri authored
365
24b3bff added ->render(text => "") support and many lite examples
sri authored
366 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
367
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
368 @@ groovy.txt.ep
369 My name is <%= $name %>.
24b3bff added ->render(text => "") support and many lite examples
sri authored
370
124ea7e reformatted lite examples to a tutorial
sri authored
371 All those features can be easily used together.
24b3bff added ->render(text => "") support and many lite examples
sri authored
372
124ea7e reformatted lite examples to a tutorial
sri authored
373 # /everything/*?name=*
24b3bff added ->render(text => "") support and many lite examples
sri authored
374 get '/everything/:stuff' => [stuff => qr/\d+/] => {stuff => 23} => sub {
c8d4f8f updated pod
Sebastian Riedel authored
375 shift->render('welcome');
24b3bff added ->render(text => "") support and many lite examples
sri authored
376 };
124ea7e reformatted lite examples to a tutorial
sri authored
377
24b3bff added ->render(text => "") support and many lite examples
sri authored
378 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
379
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
380 @@ welcome.html.ep
381 Stuff is <%= $stuff %>.
382 Query param name is <%= param 'name' %>.
24b3bff added ->render(text => "") support and many lite examples
sri authored
383
124ea7e reformatted lite examples to a tutorial
sri authored
384 Here's a fully functional example for a html form handling application using
385 multiple features at once.
386
387 #!/usr/bin/env perl
388
389 use Mojolicious::Lite;
390
391 get '/' => 'index';
392
eee70d7 fixed typo in tutorial
Sebastian Riedel authored
393 post '/form' => sub {
124ea7e reformatted lite examples to a tutorial
sri authored
394 my $self = shift;
395
b26e6b6 added param to Mojolicious::Controller
Sebastian Riedel authored
396 my $groovy = $self->param('groovy') || 'Austin Powers';
124ea7e reformatted lite examples to a tutorial
sri authored
397 $groovy =~ s/[^\w\s]+//g;
398
399 $self->render(
400 template => 'welcome',
401 layout => 'funky',
402 groovy => $groovy
403 );
eee70d7 fixed typo in tutorial
Sebastian Riedel authored
404 } => 'form';
124ea7e reformatted lite examples to a tutorial
sri authored
405
406 shagadelic;
407 __DATA__
408
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
409 @@ index.html.ep
d70b69f added session and signed cookie support
Sebastian Riedel authored
410 % layout 'funky';
124ea7e reformatted lite examples to a tutorial
sri authored
411 Who is groovy?
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
412 <form action="<%= url_for 'form' %>" method="POST">
124ea7e reformatted lite examples to a tutorial
sri authored
413 <input type="text" name="groovy" />
ca088e1 fixed typo
sri authored
414 <input type="submit" value="Woosh!">
124ea7e reformatted lite examples to a tutorial
sri authored
415 </form>
416
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
417 @@ welcome.html.ep
418 <%= $groovy %> is groovy!
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
419 <%= include 'menu' %>
124ea7e reformatted lite examples to a tutorial
sri authored
420
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
421 @@ menu.html.ep
422 <a href="<%= url_for 'index' %>">Try again</a>
124ea7e reformatted lite examples to a tutorial
sri authored
423
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
424 @@ layouts/funky.html.ep
93575c8 fixed all html5 tags
Sebastian Riedel authored
425 <!doctype html><html>
97d759b cleanup
Sebastian Riedel authored
426 <head><title>Funky!</title></head>
e8cf1a7 updated tutorial with inheritance example
Sebastian Riedel authored
427 <body><%= content %>
97d759b cleanup
Sebastian Riedel authored
428 </body>
124ea7e reformatted lite examples to a tutorial
sri authored
429 </html>
430
e6502f7 cleaned up tutorial
Sebastian Riedel authored
431 Ladders can be used for authentication and to share code between multiple
432 routes.
ab43fc9 fixed bridges/ladders and added tests
Sebastian Riedel authored
433 All routes following a ladder are only evaluated if the ladder returns a
434 true value.
435
436 use Mojolicious::Lite;
437
438 # Authenticate based on name parameter
439 ladder sub {
440 my $self = shift;
441
442 # Authenticated
443 my $name = $self->param('name') || '';
444 return 1 if $name eq 'Bender';
445
446 # Not authenticated
447 $self->render('denied');
448 return;
449 };
450
451 # GET / (with ladder authentication)
452 get '/' => 'index';
453
454 shagadelic;
455 __DATA__;
456
457 @@ denied.html.ep
458 You are not Bender, permission denied!
459
460 @@ index.html.ep
461 Hi Bender!
462
5d59ed2 fixed typo
Sebastian Riedel authored
463 Conditions such as C<agent> allow even more powerful route constructs.
2f7367e added conditions to MojoX::Routes
Sebastian Riedel authored
464
465 # /foo
466 get '/foo' => (agent => qr/Firefox/) => sub {
467 shift->render_text('Congratulations, you are using a cool browser!');
468 }
469
470 # /foo
471 get '/foo' => (agent => qr/Internet Explorer/) => sub {
472 shift->render_text('Dude, you really need to upgrade to Firefox!');
473 }
474
124ea7e reformatted lite examples to a tutorial
sri authored
475 Formats can be automatically detected by looking at file extensions.
476
477 # /detection.html
478 # /detection.txt
24b3bff added ->render(text => "") support and many lite examples
sri authored
479 get '/detection' => sub {
480 my $self = shift;
c8d4f8f updated pod
Sebastian Riedel authored
481 $self->render('detected');
24b3bff added ->render(text => "") support and many lite examples
sri authored
482 };
124ea7e reformatted lite examples to a tutorial
sri authored
483
24b3bff added ->render(text => "") support and many lite examples
sri authored
484 __DATA__
124ea7e reformatted lite examples to a tutorial
sri authored
485
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
486 @@ detected.html.ep
93575c8 fixed all html5 tags
Sebastian Riedel authored
487 <!doctype html><html>
97d759b cleanup
Sebastian Riedel authored
488 <head><title>Detected!</title></head>
489 <body>HTML was detected.</body>
24b3bff added ->render(text => "") support and many lite examples
sri authored
490 </html>
124ea7e reformatted lite examples to a tutorial
sri authored
491
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
492 @@ detected.txt.ep
24b3bff added ->render(text => "") support and many lite examples
sri authored
493 TXT was detected.
494
124ea7e reformatted lite examples to a tutorial
sri authored
495 External templates will be searched by the renderer in a C<templates>
496 directory.
497
498 # /external
23161b3 added more lite examples
sri authored
499 any '/external' => sub {
500 my $self = shift;
ad55fb5 updated pod
Sebastian Riedel authored
501
ee5d381 fixed a few cases where exceptions and not found events would result …
Sebastian Riedel authored
502 # templates/foo/bar.html.ep
c8d4f8f updated pod
Sebastian Riedel authored
503 $self->render('foo/bar');
23161b3 added more lite examples
sri authored
504 };
505
124ea7e reformatted lite examples to a tutorial
sri authored
506 Static files will be automatically served from the C<public> directory if it
507 exists.
508
23161b3 added more lite examples
sri authored
509 % mkdir public
510 % mv something.js public/something.js
511
a760263 added lite testing example
Sebastian Riedel authored
512 Testing your application is as easy as creating a C<t> directory and filling
590b113 cleanup
Sebastian Riedel authored
513 it with normal Perl unit tests like C<t/funky.t>.
a760263 added lite testing example
Sebastian Riedel authored
514
515 use Test::More tests => 3;
516 use Test::Mojo;
517
518 use FindBin;
519 require "$FindBin::Bin/../myapp.pl";
520
521 my $t = Test::Mojo->new;
522 $t->get_ok('/')->status_is(200)->content_like(qr/Funky!/);
523
524 Run all unit tests with the C<test> command.
525
526 % ./myapp.pl test
527
124ea7e reformatted lite examples to a tutorial
sri authored
528 To disable debug messages later in a production setup you can change the
529 L<Mojolicious> mode, default will be C<development>.
530
f3acdb1 added more lite examples
sri authored
531 % MOJO_MODE=production ./myapp.pl
532
124ea7e reformatted lite examples to a tutorial
sri authored
533 Log messages will be automatically written to a C<log/$mode.log> file if a
534 C<log> directory exists.
535
5e23803 fixed typo
sri authored
536 % mkdir log
537
124ea7e reformatted lite examples to a tutorial
sri authored
538 For more control the L<Mojolicious> instance can be accessed directly.
539
885a5e8 added more lite examples
sri authored
540 app->log->level('error');
541 app->routes->route('/foo/:bar')->via('get')->to(callback => sub {
542 my $self = shift;
4069892 added render_text method
Sebastian Riedel authored
543 $self->render_text('Hello Mojo!');
885a5e8 added more lite examples
sri authored
544 });
545
124ea7e reformatted lite examples to a tutorial
sri authored
546 In case a lite app needs to grow, lite and real L<Mojolicous> applications
547 can be easily mixed to make the transition process very smooth.
548
e491cf2 added example for mixed mojolicious and lite app
sri authored
549 package MyApp::Foo;
550 use base 'Mojolicious::Controller';
124ea7e reformatted lite examples to a tutorial
sri authored
551
4069892 added render_text method
Sebastian Riedel authored
552 sub index { shift->render_text('It works!') }
124ea7e reformatted lite examples to a tutorial
sri authored
553
e491cf2 added example for mixed mojolicious and lite app
sri authored
554 package main;
555 use Mojolicious::Lite;
124ea7e reformatted lite examples to a tutorial
sri authored
556
4069892 added render_text method
Sebastian Riedel authored
557 get '/bar' => sub { shift->render_text('This too!') };
124ea7e reformatted lite examples to a tutorial
sri authored
558
e491cf2 added example for mixed mojolicious and lite app
sri authored
559 app->routes->namespace('MyApp');
f5439a5 cleanup
Sebastian Riedel authored
560 app->routes->route('/foo/:action')->via('get')->to('foo#index');
a146626 added Mojolicious::Lite
sri authored
561
124ea7e reformatted lite examples to a tutorial
sri authored
562 shagadelic;
a146626 added Mojolicious::Lite
sri authored
563
564 =head1 ATTRIBUTES
565
566 L<Mojolicious::Lite> inherits all attributes from L<Mojolicious>.
567
568 =head1 METHODS
569
fcf3ef7 added the ability to have multiple Mojolicious::Lite apps at once (Mo…
Sebastian Riedel authored
570 L<Mojolicious::Lite> inherits all methods from L<Mojolicious>.
a146626 added Mojolicious::Lite
sri authored
571
d49a4ee pod cleanup
Sebastian Riedel authored
572 =head1 SEE ALSO
573
3387874 renamed Mojolicious::Book to Mojolicious::Guides
Sebastian Riedel authored
574 L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
d49a4ee pod cleanup
Sebastian Riedel authored
575
a146626 added Mojolicious::Lite
sri authored
576 =cut
Something went wrong with that request. Please try again.