Skip to content

Instantly share code, notes, and snippets.

View kraih's full-sized avatar

Sebastian Riedel kraih

View GitHub Profile
# Simple "resource" shortcut
$r->add_shortcut(resource => sub {
my ($r, $name) = @_;
# Generate "/$name" route
my $resource = $r->route("/$name")->to("$name#");
# Handle POST requests
$resource->post->to('#create')->name("create_$name");
% mojo get http://mojolicio.us 'head > title'
<title>Mojolicious Web Framework - Join the Perl revolution!</title>
% mojo get http://mojolicio.us 'a[href]' attr href
http://latest.mojolicio.us
http://mojolicio.us
http://mojolicio.us/perldoc
https://github.com/kraih/mojo/wiki
https://github.com/kraih/mojo
http://search.cpan.org/dist/Mojolicious
http://groups.google.com/group/mojolicious
http://blog.kraih.com
http://twitter.com/kraih
% mojo generate lite_app
[exist] /Users/sri
[write] /Users/sri/myapp.pl
[chmod] myapp.pl 744
% ./myapp.pl routes
/perldoc perldoc (?-xism:^/perldoc)
/welcome welcome (?-xism:^/welcome)
% mojo generate lite_app
[exist] /Users/sri
[write] /Users/sri/myapp.pl
[chmod] myapp.pl 744
% ./myapp.pl get --verbose --mode testing /welcome 'head > title' text
GET /welcome HTTP/1.1
User-Agent: Mojolicious (Perl)
Content-Length: 0
Host: localhost:13359
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/welcome';
app->start;
__DATA__
@@ welcome.html.ep
% ./myapp.pl get --mode testing / html all
Not Found
Page not found, want to go home?
% ./myapp.pl get --mode production / html all
Dude!Where is my page?
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/welcome' => sub {
my $self = shift;
$self->render(text => 'Hi there!');
};
app->start;
#!/usr/bin/env perl
use Mojolicious::Routes;
use Mojolicious::Routes::Match;
# Create some routes
my $r = Mojolicious::Routes->new;
$r->get('/:action')->to(controller => 'foo');
# Match method and path against routes
% ./myapp.pl daemon --listen http://[::1]:3000
Thu Mar 10 11:26:59 2011 info Mojo::Server::Daemon:316 [1576]: Server listening (http://[::1]:3000)