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