Mojolicious::Lite - Micro real-time web framework
SYNOPSIS
# Automatically enables "strict", "warnings", "utf8" and Perl 5.16 features | |
use Mojolicious::Lite -signatures; | |
# Route with placeholder | |
get '/:foo' => sub ($c) { | |
my $foo = $c->param('foo'); | |
$c->render(text => "Hello from $foo."); | |
}; | |
# Start the Mojolicious command system | |
app->start; |
DESCRIPTION
Mojolicious::Lite is a tiny domain specific language built around Mojolicious, made up of only about a dozen Perl functions.
On Perl 5.20+ you can also use a -signatures
flag to enable support for subroutine signatures.
use Mojolicious::Lite -signatures; | |
get '/:foo' => sub ($c) { | |
my $foo = $c->param('foo'); | |
$c->render(text => "Hello from $foo."); | |
}; | |
app->start; |
See Mojolicious::Guides::Tutorial for more!
GROWING
While Mojolicious::Guides::Growing will give you a detailed introduction to growing a Mojolicious::Lite prototype into a well-structured Mojolicious application, here we have collected a few snippets that illustrate very well just how similar both of them are.
Routes
The functions "get", "post" and friends all have equivalent methods.
# Mojolicious::Lite | |
get '/foo' => sub ($c) { | |
$c->render(text => 'Hello World!'); | |
}; | |
# Mojolicious | |
sub startup ($self) { | |
my $routes = $self->routes; | |
$routes->get('/foo' => sub ($c) { | |
$c->render(text => 'Hello World!'); | |
}); | |
} |
Application
The application object you can access with the function "app" is the first argument passed to the startup
method.
# Mojolicious::Lite | |
app->max_request_size(16777216); | |
# Mojolicious | |
sub startup ($self) { | |
$self->max_request_size(16777216); | |
} |
Plugins
Instead of the "plugin" function you just use the method "plugin" in Mojolicious.
# Mojolicious::Lite | |
plugin 'Config'; | |
# Mojolicious | |
sub startup ($self) { | |
$self->plugin('Config'); | |
} |
Helpers
Similar to plugins, instead of the "helper" function you just use the method "helper" in Mojolicious.
# Mojolicious::Lite | |
helper two => sub ($c) { | |
return 1 + 1; | |
}; | |
# Mojolicious | |
sub startup ($self) { | |
$self->helper(two => sub ($c) { | |
return 1 + 1; | |
}); | |
} |
Under
Instead of sequential function calls, we can use methods to build a tree with nested routes, that much better illustrates how routes work internally.
# Mojolicious::Lite | |
under '/foo'; | |
get '/bar' => sub ($c) {...}; | |
# Mojolicious | |
sub startup ($self) { | |
my $routes = $self->routes; | |
my $foo = $routes->under('/foo'); | |
$foo->get('/bar' => sub ($c) {...}); | |
} |
FUNCTIONS
Mojolicious::Lite implements the following functions, which are automatically exported.
any
my $route = any '/:foo' => sub ($c) {...}; | |
my $route = any '/:foo' => sub ($c) {...} => 'name'; | |
my $route = any '/:foo' => {foo => 'bar'} => sub ($c) {...}; | |
my $route = any '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = any ['GET', 'POST'] => '/:foo' => sub ($c) {...}; | |
my $route = any ['GET', 'POST'] => '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = any ['GET', 'POST'] => '/:foo' => (agent => qr/Firefox/) => sub ($c) {...}; |
Generate route with "any" in Mojolicious::Routes::Route, matching any of the listed HTTP request methods or all. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
app
my $app = app;
Returns the Mojolicious::Lite application object, which is a subclass of Mojolicious.
# Use all the available attributes and methods | |
app->log->level('error'); | |
app->defaults(foo => 'bar'); |
del
my $route = del '/:foo' => sub ($c) {...}; | |
my $route = del '/:foo' => sub ($c) {...} => 'name'; | |
my $route = del '/:foo' => {foo => 'bar'} => sub ($c) {...}; | |
my $route = del '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = del '/:foo' => (agent => qr/Firefox/) => sub ($c) {...}; |
Generate route with "delete" in Mojolicious::Routes::Route, matching only DELETE
requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
get
my $route = get '/:foo' => sub ($c) {...}; | |
my $route = get '/:foo' => sub ($c) {...} => 'name'; | |
my $route = get '/:foo' => {foo => 'bar'} => sub ($c) {...}; | |
my $route = get '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = get '/:foo' => (agent => qr/Firefox/) => sub ($c) {...}; |
Generate route with "get" in Mojolicious::Routes::Route, matching only GET
requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
group
group {...};
Start a new route group.
helper
helper foo => sub ($c, @args) {...};
Add a new helper with "helper" in Mojolicious.
hook
hook after_dispatch => sub ($c) {...};
Share code with "hook" in Mojolicious.
options
my $route = options '/:foo' => sub ($c) {...}; | |
my $route = options '/:foo' => sub ($c) {...} => 'name'; | |
my $route = options '/:foo' => {foo => 'bar'} => sub ($c) {...}; | |
my $route = options '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = options '/:foo' => (agent => qr/Firefox/) => sub ($c) {...}; |
Generate route with "options" in Mojolicious::Routes::Route, matching only OPTIONS
requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
patch
my $route = patch '/:foo' => sub ($c) {...}; | |
my $route = patch '/:foo' => sub ($c) {...} => 'name'; | |
my $route = patch '/:foo' => {foo => 'bar'} => sub ($c) {...}; | |
my $route = patch '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = patch '/:foo' => (agent => qr/Firefox/) => sub ($c) {...}; |
Generate route with "patch" in Mojolicious::Routes::Route, matching only PATCH
requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
plugin
plugin SomePlugin => {foo => 23};
Load a plugin with "plugin" in Mojolicious.
post
my $route = post '/:foo' => sub ($c) {...}; | |
my $route = post '/:foo' => sub ($c) {...} => 'name'; | |
my $route = post '/:foo' => {foo => 'bar'} => sub ($c) {...}; | |
my $route = post '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = post '/:foo' => (agent => qr/Firefox/) => sub ($c) {...}; |
Generate route with "post" in Mojolicious::Routes::Route, matching only POST
requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
put
my $route = put '/:foo' => sub ($c) {...}; | |
my $route = put '/:foo' => sub ($c) {...} => 'name'; | |
my $route = put '/:foo' => {foo => 'bar'} => sub ($c) {...}; | |
my $route = put '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = put '/:foo' => (agent => qr/Firefox/) => sub ($c) {...}; |
Generate route with "put" in Mojolicious::Routes::Route, matching only PUT
requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
under
my $route = under sub ($c) {...}; | |
my $route = under '/:foo' => sub ($c) {...}; | |
my $route = under '/:foo' => {foo => 'bar'}; | |
my $route = under '/:foo' => [foo => qr/\w+/]; | |
my $route = under '/:foo' => (agent => qr/Firefox/); | |
my $route = under [format => ['json', 'yaml']]; |
Generate nested route with "under" in Mojolicious::Routes::Route, to which all following routes are automatically appended. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
websocket
my $route = websocket '/:foo' => sub ($c) {...}; | |
my $route = websocket '/:foo' => sub ($c) {...} => 'name'; | |
my $route = websocket '/:foo' => {foo => 'bar'} => sub ($c) {...}; | |
my $route = websocket '/:foo' => [foo => qr/\w+/] => sub ($c) {...}; | |
my $route = websocket '/:foo' => (agent => qr/Firefox/) => sub ($c) {...}; |
Generate route with "websocket" in Mojolicious::Routes::Route, matching only WebSocket handshakes. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information.
ATTRIBUTES
Mojolicious::Lite inherits all attributes from Mojolicious.
METHODS
Mojolicious::Lite inherits all methods from Mojolicious.