1Mojolicious(3) User Contributed Perl Documentation Mojolicious(3)
2
3
4
6 Mojolicious - The Web In A Box!
7
9 # Mojolicious application
10 package MyApp;
11
12 use base 'Mojolicious';
13
14 sub startup {
15 my $self = shift;
16
17 # Routes
18 my $r = $self->routes;
19
20 # Default route
21 $r->route('/:controller/:action/:id')->to('foo#welcome');
22 }
23
24 # Mojolicious controller
25 package MyApp::Foo;
26
27 use base 'Mojolicious::Controller';
28
29 # Say hello
30 sub welcome {
31 my $self = shift;
32 $self->render_text('Hi there!');
33 }
34
35 # Say goodbye from a template (foo/bye.html.ep)
36 sub bye { shift->render }
37
39 Back in the early days of the web there was this wonderful Perl library
40 called CGI, many people only learned Perl because of it. It was simple
41 enough to get started without knowing much about the language and
42 powerful enough to keep you going, learning by doing was much fun.
43 While most of the techniques used are outdated now, the idea behind it
44 is not. Mojolicious is a new attempt at implementing this idea using
45 state of the art technology.
46
47 Features
48 An amazing MVC web framework supporting a simplified single file
49 mode through Mojolicious::Lite.
50
51 Powerful out of the box with RESTful routes, plugins, Perl-ish
52 templates, session management, signed cookies, testing
53 framework, static file server, I18N, first class unicode
54 support and much more for you to discover.
55
56 Very clean, portable and Object Oriented pure Perl API without any
57 hidden magic and no requirements besides Perl 5.8.7.
58
59 Full stack HTTP 1.1 and WebSocket client/server implementation with
60 IPv6, TLS, Bonjour, IDNA, chunking and multipart support.
61
62 Builtin async IO and prefork web server supporting epoll, kqueue,
63 hot deployment and UNIX domain socket sharing, perfect for
64 embedding.
65
66 Automatic CGI, FastCGI and PSGI detection.
67
68 JSON and XML/HTML5 parser with CSS3 selector support.
69
70 Fresh code based upon years of experience developing Catalyst.
71
72 Duct Tape For The HTML5 Web
73 Web development for humans, making hard things possible and everything
74 fun.
75
76 use Mojolicious::Lite;
77
78 get '/hello' => sub { shift->render(text => 'Hello World!') }
79
80 get '/time' => 'clock';
81
82 websocket '/echo' => sub {
83 my $self = shift;
84 $self->receive_message(
85 sub {
86 my ($self, $message) = @_;
87 $self->send_message("echo: $message");
88 }
89 );
90 };
91
92 get '/title' => sub {
93 my $self = shift;
94 my $url = $self->param('url');
95 $self->render(text =>
96 $self->client->get($url)->res->dom->at('title')->text);
97 };
98
99 post '/:offset' => sub {
100 my $self = shift;
101 my $offset = $self->param('offset') || 23;
102 $self->render(json => {list => [0 .. $offset]});
103 };
104
105 app->start;
106 __DATA__
107
108 @@ clock.html.ep
109 % my ($second, $minute, $hour) = (localtime(time))[0, 1, 2];
110 The time is <%= $hour %>:<%= $minute %>:<%= $second %>.
111
112 For more user friendly documentation see Mojolicious::Guides and
113 Mojolicious::Lite.
114
115 Have Some Cake
116 .---------------------------------------------------------------.
117 | Fun! |
118 '---------------------------------------------------------------'
119 .---------------------------------------------------------------.
120 | |
121 | .----------------------------------------------'
122 | | .--------------------------------------------.
123 | Application | | Mojolicious::Lite |
124 | | '--------------------------------------------'
125 | | .--------------------------------------------.
126 | | | Mojolicious |
127 '----------------' '--------------------------------------------'
128 .---------------------------------------------------------------.
129 | Mojo |
130 '---------------------------------------------------------------'
131 .-------. .-----------. .--------. .------------. .-------------.
132 | CGI | | FastCGI | | PSGI | | HTTP 1.1 | | WebSocket |
133 '-------' '-----------' '--------' '------------' '-------------'
134
136 Mojolicious inherits all attributes from Mojo and implements the
137 following new ones.
138
139 "controller_class"
140 my $class = $mojo->controller_class;
141 $mojo = $mojo->controller_class('Mojolicious::Controller');
142
143 Class to be used for the default controller, defaults to
144 Mojolicious::Controller.
145
146 "mode"
147 my $mode = $mojo->mode;
148 $mojo = $mojo->mode('production');
149
150 The operating mode for your application. It defaults to the value of
151 the environment variable "MOJO_MODE" or "development". Mojo will name
152 the log file after the current mode and modes other than "development"
153 will result in limited log output.
154
155 If you want to add per mode logic to your application, you can add a
156 sub to your application named $mode_mode.
157
158 sub development_mode {
159 my $self = shift;
160 }
161
162 sub production_mode {
163 my $self = shift;
164 }
165
166 "plugins"
167 my $plugins = $mojo->plugins;
168 $mojo = $mojo->plugins(Mojolicious::Plugins->new);
169
170 The plugin loader, by default a Mojolicious::Plugins object. You can
171 usually leave this alone, see Mojolicious::Plugin if you want to write
172 a plugin.
173
174 "renderer"
175 my $renderer = $mojo->renderer;
176 $mojo = $mojo->renderer(MojoX::Renderer->new);
177
178 Used in your application to render content, by default a
179 MojoX::Renderer object. The two main renderer plugins
180 Mojolicious::Plugin::EpRenderer and Mojolicious::Plugin::EplRenderer
181 contain more specific information.
182
183 "routes"
184 my $routes = $mojo->routes;
185 $mojo = $mojo->routes(MojoX::Dispatcher::Routes->new);
186
187 The routes dispatcher, by default a MojoX::Dispatcher::Routes object.
188 You use this in your startup method to define the url endpoints for
189 your application.
190
191 sub startup {
192 my $self = shift;
193
194 my $r = $self->routes;
195 $r->route('/:controller/:action')->to('test#welcome');
196 }
197
198 "secret"
199 my $secret = $mojo->secret;
200 $mojo = $mojo->secret('passw0rd');
201
202 A secret passphrase used for signed cookies and the like, defaults to
203 the application name which is not very secure, so you should change
204 it!!! As long as you are using the unsecure default there will be
205 debug messages in the log file reminding you to change your passphrase.
206
207 "static"
208 my $static = $mojo->static;
209 $mojo = $mojo->static(MojoX::Dispatcher::Static->new);
210
211 For serving static assets from your "public" directory, by default a
212 MojoX::Dispatcher::Static object.
213
214 "types"
215 my $types = $mojo->types;
216 $mojo = $mojo->types(MojoX::Types->new);
217
218 Responsible for tracking the types of content you want to serve in your
219 application, by default a MojoX::Types object. You can easily register
220 new types.
221
222 $mojo->types->type(vti => 'help/vampire');
223
225 Mojolicious inherits all methods from Mojo and implements the following
226 new ones.
227
228 "new"
229 my $mojo = Mojolicious->new;
230
231 Construct a new Mojolicious application. Will automatically detect
232 your home directory and set up logging based on your current operating
233 mode. Also sets up the renderer, static dispatcher and a default set
234 of plugins.
235
236 "defaults"
237 my $defaults = $mojo->default;
238 my $foo = $mojo->defaults('foo');
239 $mojo = $mojo->defaults({foo => 'bar'});
240 $mojo = $mojo->defaults(foo => 'bar');
241
242 Default values for the stash. Note that this method is EXPERIMENTAL
243 and might change without warning!
244
245 $mojo->defaults->{foo} = 'bar';
246 my $foo = $mojo->defaults->{foo};
247 delete $mojo->defaults->{foo};
248
249 "dispatch"
250 $mojo->dispatch($c);
251
252 The heart of every Mojolicious application, calls the static and routes
253 dispatchers for every request.
254
255 "finish"
256 $mojo->finish($c);
257
258 Clean up after processing a request, usually called automatically.
259
260 "handler"
261 $tx = $mojo->handler($tx);
262
263 Sets up the default controller and calls process for every request.
264
265 "plugin"
266 $mojo->plugin('something');
267 $mojo->plugin('something', foo => 23);
268 $mojo->plugin('something', {foo => 23});
269
270 Load a plugin.
271
272 "process"
273 $mojo->process($c);
274
275 This method can be overloaded to do logic on a per request basis, by
276 default just calls dispatch. Generally you will use a plugin or
277 controller instead of this, consider it the sledgehammer in your
278 toolbox.
279
280 sub process {
281 my ($self, $c) = @_;
282 $self->dispatch($c);
283 }
284
285 "start"
286 Mojolicious->start;
287 Mojolicious->start('daemon');
288
289 Start the Mojolicious::Commands command line interface for your
290 application.
291
292 "startup"
293 $mojo->startup;
294
295 This is your main hook into the application, it will be called at
296 application startup.
297
298 sub startup {
299 my $self = shift;
300 }
301
303 Web
304 http://mojolicious.org
305
306 IRC
307 #mojo on irc.perl.org
308
309 Mailing-List
310 http://groups.google.com/group/mojolicious
311
313 Repository
314 http://github.com/kraih/mojo
315
317 Sebastian Riedel, "sri@cpan.org".
318
320 Viacheslav Tykhanovskyi, "vti@cpan.org".
321
323 In alphabetical order:
324
325 Adam Kennedy
326
327 Adriano Ferreira
328
329 Alex Salimon
330
331 Alexey Likhatskiy
332
333 Anatoly Sharifulin
334
335 Andre Vieth
336
337 Andrew Fresh
338
339 Andreas Koenig
340
341 Andy Grundman
342
343 Aristotle Pagaltzis
344
345 Ashley Dev
346
347 Ask Bjoern Hansen
348
349 Audrey Tang
350
351 Breno G. de Oliveira
352
353 Burak Gursoy
354
355 Ch Lamprecht
356
357 Christian Hansen
358
359 Curt Tilmes
360
361 Danijel Tasov
362
363 David Davis
364
365 Dmitry Konstantinov
366
367 Eugene Toropov
368
369 Gisle Aas
370
371 Glen Hinkle
372
373 Graham Barr
374
375 Hideki Yamamura
376
377 James Duncan
378
379 Jaroslav Muhin
380
381 Jesse Vincent
382
383 John Kingsley
384
385 Jonathan Yu
386
387 Kazuhiro Shibuya
388
389 Kevin Old
390
391 Lars Balker Rasmussen
392
393 Leon Brocard
394
395 Maik Fischer
396
397 Marcus Ramberg
398
399 Mark Stosberg
400
401 Matthew Lineen
402
403 Maksym Komar
404
405 Maxim Vuets
406
407 Mirko Westermeier
408
409 Oleg Zhelo
410
411 Pascal Gaudette
412
413 Pedro Melo
414
415 Peter Edwards
416
417 Pierre-Yves Ritschard
418
419 Quentin Carbonneaux
420
421 Rafal Pocztarski
422
423 Randal Schwartz
424
425 Robert Hicks
426
427 Ryan Jendoubi
428
429 Sascha Kiefer
430
431 Sergey Zasenko
432
433 Shu Cho
434
435 Stanis Trendelenburg
436
437 Tatsuhiko Miyagawa
438
439 The Perl Foundation
440
441 Tomas Znamenacek
442
443 Ulrich Kautz
444
445 Uwe Voelker
446
447 Yaroslav Korshak
448
449 Yuki Kimoto
450
451 Zak B. Elep
452
454 Copyright (C) 2008-2010, Sebastian Riedel.
455
456 This program is free software, you can redistribute it and/or modify it
457 under the terms of the Artistic License version 2.0.
458
459
460
461perl v5.12.3 2010-08-17 Mojolicious(3)