(from
the blog)
This is the biggest and most exciting
Mojo update so far, because it finally includes the first example web framework named
Mojolicious.
Mojolicious has many similarities to
Ruby on Rails and
Merb, but stays true to it's Perl roots.
While it's still just a proof of concept, it shows very well whats so cool about
Mojo and how to get started with building web frameworks on top of it.
Trying
Mojolicious is very simple thanks to the helpers and code generator shipped with it.
$ cpanp install Mojo
... no prereqs needed, so installation will be very fast ...
$ mojolicious generate app MyMojoliciousApp
[mkdir] /Users/sri/my_mojolicious_app/bin
[write] /Users/sri/my_mojolicious_app/bin/mojolicious
[chmod] my_mojolicious_app/bin/mojolicious 744
[mkdir] /Users/sri/my_mojolicious_app/lib
[write] /Users/sri/my_mojolicious_app/lib/MyMojoliciousApp.pm
[mkdir] /Users/sri/my_mojolicious_app/lib/MyMojoliciousApp
[write] /Users/sri/my_mojolicious_app/lib/MyMojoliciousApp/Example.p
+m
[mkdir] /Users/sri/my_mojolicious_app/t
[write] /Users/sri/my_mojolicious_app/t/basic.t
[mkdir] /Users/sri/my_mojolicious_app/public
[write] /Users/sri/my_mojolicious_app/public/404.html
[exist] /Users/sri/my_mojolicious_app/public
[write] /Users/sri/my_mojolicious_app/public/index.html
[mkdir] /Users/sri/my_mojolicious_app/templates/example
[write] /Users/sri/my_mojolicious_app/templates/example/welcome.phtm
+l
$ cd my_mojolicious_app
$ bin/mojolicious daemon
Server available at http://127.0.0.1:3000.
By setting the MOJO_RELOAD environment variable you can just live edit every single file, web application development has never been this simple.
$ MOJO_RELOAD=1 bin/mojolicious daemon
Server available at http://127.0.0.1:3000.
Unlinke other frameworks
Mojolicious won't try to hide the dispatcher logic from you.
The application class gives you total control.
package MyMojoliciousApp;
use strict;
use warnings;
use base 'Mojolicious';
# This method will run for each request
sub dispatch {
my ($self, $c) = @_;
# Try to find a static file
$self->static->dispatch($c);
# Use routes if we don't have a response code yet
$self->routes->dispatch($c) unless $c->res->code;
# Nothing found
unless ($c->res->code) {
$self->static->serve($c, '/404.html');
$c->res->code(404);
}
}
# This method will run once at server start
sub startup {
my $self = shift;
# The routes
my $r = $self->routes;
# Default route
$r->route('/:controller/:action/:id')
->to(controller => 'example', action => 'welcome', id => 1);
}
1;
Controllers are plain old Perl classes without any magic.
package MyMojoliciousApp::Example;
use strict;
use warnings;
use base 'Mojolicious::Controller';
# This is a templateless action
sub test {
my ($self, $c) = @_;
# Response object
my $res = $c->res;
# Code
$res->code(200);
# Headers
$res->headers->content_type('text/html');
# Content
my $url = $c->url_for;
$url->path->parse('/index.html');
$res->body(qq/<a href="$url">Forward to a static document.<\/a>/);
}
# This action will render a template
sub welcome {
my ($self, $c) = @_;
# Render the template
$c->render;
}
1;
By default
Mojo::Template (also known as "phtml") will be used to render templates.
But
Mojolicious was designed to use multiple parallel template engines. (File extension decides which one to use)
% my $c = shift;
<h2>Welcome to the Mojolicious Web Framework!</h2>
This page was generated from a template at templates/example/test.phtm
+l,
<a href="<%= $c->url_for(action => 'test') %>">click here</a>
to move forward to a templateless action.
Mojo itself also got some big updates, here's the complete changelog.
- Added the Mojolicious Web Framework example.
- Added upload and GET/POST parameter helpers to Mojo::Message.
- Hooks for upload progress and stuff added.
- Refactored transfer encoding code into Mojo::Filter and
Mojo::Filter::Chunked.
- Added callbacks for start line and header generators.
- Added workaround for missing IO::Seekable support in older
versions of File::Temp (Perl 5.8).
- script/mojo.pl got renamed to bin/mojo.
- Mojo::Cache got renamed to Mojo::File because there will be a cache
module named MojoX::Cache, and that could cause confusion later on.
- Fixed many escaping related bugs around Mojo::URL.
- Fixed 100-Continue support in Mojo::Server::Daemon and Mojo::Client.
- Countless small bugs fixed and tests added.
Like what you see here? Now would be the perfect time to get involved in the project!
Just join our
mailing list or the irc channel (#mojo on irc.perl.org) and share your ideas with us.