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