Mojolicious - The Web In A Box!

Author: Sebastian Riedel (kraih). Link to original: http://search.cpan.org/~kraih/Mojolicious-0.999926/lib/Mojolicious.pm (English).
Tags: mojolicious, Perl Submitted by perl5doc.ru 12.07.2010. Public material.

Translations of this material:

into Russian: Mojolicious — веб в коробке!. Translation complete.
Submitted for translation by perl5doc.ru 12.07.2010 Published 1 year, 10 months ago.

Text

=head1 NAME

Mojolicious - The Web In A Box!

=head1 SYNOPSIS

# Mojolicious application
package MyApp;

use base 'Mojolicious';

sub startup {
my $self = shift;

# Routes
my $r = $self->routes;

# Default route
$r->route('/:controller/:action/:id')->to('foo#welcome');
}

# Mojolicious controller
package MyApp::Foo;

use base 'Mojolicious::Controller';

# Say hello
sub welcome {
my $self = shift;
$self->render_text('Hi there!');
}

# Say goodbye from a template (foo/bye.html.ep)
sub bye { shift->render }

=head1 DESCRIPTION

Back in the early days of the web there was this wonderful Perl library
called L<CGI>, many people only learned Perl because of it.
It was simple enough to get started without knowing much about the language
and powerful enough to keep you going, learning by doing was much fun.
While most of the techniques used are outdated now, the idea behind it is
not.
L<Mojolicious> is a new attempt at implementing this idea using state of the
art technology.

=head2 Features

=over 4

An amazing MVC web framework supporting a simplified single file mode through
L<Mojolicious::Lite>.

Very clean, portable and Object Oriented pure Perl API without any hidden
magic and no requirements besides Perl 5.8.7.

Full stack HTTP 1.1 and WebSocket client/server implementation with IPv6,
TLS, IDNA, pipelining, chunking and multipart support.

Builtin async IO and prefork web server supporting epoll, kqueue, hot
deployment and UNIX domain socket sharing, perfect for embedding.

CGI, FastCGI and L<PSGI> support.

Fresh code, based upon years of experience developing Catalyst.

Powerful out of the box with RESTful routes, plugins, sessions, signed
cookies, static file server, testing framework, Perl-ish templates, JSON,
I18N, first class Unicode support and much more for you to discover!

=back

=head2 Duct Tape For The HTML5 Web

Web development for humans, making hard things possible and everything fun.

use Mojolicious::Lite;

get '/hello' => sub { shift->render(text => 'Hello World!') }

get '/time' => 'clock';

websocket '/echo' => sub {
my $self = shift;
$self->receive_message(
sub {
my ($self, $message) = @_;
$self->send_message("echo: $message");
}
);
};

get '/title' => sub {
my $self = shift;
my $url = $self->param('url');
$self->render(text =>
$self->client->get($url)->success->dom->at('title')->text);
};

post '/:offset' => sub {
my $self = shift;
my $offset = $self->param('offset') || 23;
$self->render(json => {list => [0 .. $offset]});
};

app->start;
__DATA__

@@ clock.html.ep
% my ($second, $minute, $hour) = (localtime(time))[0, 1, 2];
The time is <%= $hour %>:<%= $minute %>:<%= $second %>.

For more user friendly documentation see L<Mojolicious::Guides> and
L<Mojolicious::Lite>.

=head2 Have Some Cake

.---------------------------------------------------------------.
| Fun! |
'---------------------------------------------------------------'
.---------------------------------------------------------------.
| |
| .----------------------------------------------'
| | .--------------------------------------------.
| Application | | Mojolicious::Lite |
| | '--------------------------------------------'
| | .--------------------------------------------.
| | | Mojolicious |
'----------------' '--------------------------------------------'
.---------------------------------------------------------------.
| Mojo |
'---------------------------------------------------------------'
.-------. .-----------. .--------. .------------. .-------------.
| CGI | | FastCGI | | PSGI | | HTTP 1.1 | | WebSocket |
'-------' '-----------' '--------' '------------' '-------------'

=head1 ATTRIBUTES

L<Mojolicious> inherits all attributes from L<Mojo> and implements the
following new ones.

=head2 C<controller_class>

my $class = $mojo->controller_class;
$mojo = $mojo->controller_class('Mojolicious::Controller');

Class to be used for the default controller, defaults to
L<Mojolicious::Controller>.

=head2 C<mode>

my $mode = $mojo->mode;
$mojo = $mojo->mode('production');

The operating mode for your application.
It defaults to the value of the environment variable C<MOJO_MODE> or
C<development>.
Mojo will name the log file after the current mode and modes other than
C<development> will result in limited log output.

If you want to add per mode logic to your application, you can add a sub
to your application named C<$mode_mode>.

sub development_mode {
my $self = shift;
}

sub production_mode {
my $self = shift;
}

=head2 C<plugins>

my $plugins = $mojo->plugins;
$mojo = $mojo->plugins(Mojolicious::Plugins->new);

The plugin loader, by default a L<Mojolicious::Plugins> object.
You can usually leave this alone, see L<Mojolicious::Plugin> if you want to
write a plugin.

=head2 C<renderer>

my $renderer = $mojo->renderer;
$mojo = $mojo->renderer(MojoX::Renderer->new);

Used in your application to render content, by default a L<MojoX::Renderer>
object.
The two main renderer plugins L<Mojolicious::Plugin::EpRenderer> and
L<Mojolicious::Plugin::EplRenderer> contain more specific information.

=head2 C<routes>

my $routes = $mojo->routes;
$mojo = $mojo->routes(MojoX::Dispatcher::Routes->new);

The routes dispatcher, by default a L<MojoX::Dispatcher::Routes> object.
You use this in your startup method to define the url endpoints for your
application.

sub startup {
my $self = shift;

my $r = $self->routes;
$r->route('/:controller/:action')->to('test#welcome');
}

=head2 C<secret>

my $secret = $mojo->secret;
$mojo = $mojo->secret('passw0rd');

A secret passphrase used for signed cookies and the like, defaults to the
application name which is not very secure, so you should change it!!!
As long as you are using the unsecure default there will be debug messages in
the log file reminding you to change your passphrase.

=head2 C<static>

my $static = $mojo->static;
$mojo = $mojo->static(MojoX::Dispatcher::Static->new);

For serving static assets from your C<public> directory, by default a
L<MojoX::Dispatcher::Static> object.

=head2 C<types>

my $types = $mojo->types;
$mojo = $mojo->types(MojoX::Types->new);

Responsible for tracking the types of content you want to serve in your
application, by default a L<MojoX::Types> object.
You can easily register new types.

$mojo->types->type(vti => 'help/vampire');

=head1 METHODS

L<Mojolicious> inherits all methods from L<Mojo> and implements the following
new ones.

=head2 C<new>

my $mojo = Mojolicious->new;

Construct a new L<Mojolicious> application.
Will automatically detect your home directory and set up logging based on
your current operating mode.
Also sets up the renderer, static dispatcher and a default set of plugins.

=head2 C<dispatch>

$mojo->dispatch($c);

The heart of every Mojolicious application, calls the static and routes
dispatchers for every request.

=head2 C<finish>

$mojo->finish($c);

Clean up after processing a request, usually called automatically.

=head2 C<handler>

$tx = $mojo->handler($tx);

Sets up the default controller and calls process for every request.

=head2 C<plugin>

$mojo->plugin('something');
$mojo->plugin('something', foo => 23);
$mojo->plugin('something', {foo => 23});

Load a plugin.

=head2 C<process>

$mojo->process($c);

This method can be overloaded to do logic on a per request basis, by default
just calls dispatch.
Generally you will use a plugin or controller instead of this, consider it
the sledgehammer in your toolbox.

sub process {
my ($self, $c) = @_;
$self->dispatch($c);
}

=head2 C<start>

Mojolicious->start;
Mojolicious->start('daemon');

Start the L<Mojolicious::Commands> command line interface for your
application.

=head2 C<startup>

$mojo->startup;

This is your main hook into the application, it will be called at application
startup.

sub startup {
my $self = shift;
}

=head1 SUPPORT

=head2 Web

http://mojolicious.org

Pages: ← previous Ctrl next
1 2