mojolicious/0000775000540200054020000000000012035156137013015 5ustar travistravismojolicious/mojolicious.html0000664000540200054020000002274512035155361016247 0ustar travistravis {{Mojolicious}} The bits Lenz didn't talk about yet

Mojolicious
The bits Lenz didn't talk about yet

Mojolicious

  • MVC framework alternative to Catalyst
  • Written by Sebastian Riedel one of the original Catalyst authors
  • Bootstrapping
  • MVC stuff
  • Off and running
  • Routing
  • Template system
  • 1. Bootstrapping

    Create Your Application

    $ mojo generate app MyApp [mkdir] ~/my_app/script [write] ~/my_app/script/my_app [chmod] my_app/script/my_app 744 [mkdir] ~/my_app/lib [write] ~/my_app/lib/MyApp.pm [mkdir] ~/my_app/lib/MyApp [write] ~/my_app/lib/MyApp/Example.pm . . .

    Basic File structure

    2. Off and running

    Development server

  • out of the box
  • $ ./script/my_app -l http://*:3000 [-l https://*:443]

  • ..better
  • $ morbo script/my_app -l http://*:3000 [-l https://*:443]

    Production Server

  • various options
  • plack
  • $ plackup ./script/my_app HTTP::Server::PSGI: Accepting connections at http://0:5000/

  • hypnotoad
  • $ hypnotoad script/my_app Server available at http://127.0.0.1:8080. + preforking server + supports hot deployment

    Sample server config

  • hypnotoad and nginx
  • upstream myapp { server 127.0.0.1:8080; } server { listen 80; server_name localhost; location / { proxy_read_timeout 300; proxy_pass http://myapp; proxy_set_header Host $host; proxy_set_header X−Forwarded−For \ $proxy_add_x_forwarded_for; proxy_set_header X−Forwarded−HTTPS 0; } }

    3.Routing

    Basic Routing

  • http://myapp.com/users/list
  • my $r = $self->route(); $r->route('/users/list')->to('users#list'); # lib/MyApp/Users.pm # sub list {}

  • http://myapp.com/users/create
  • $r->post('/users/create')->to('users#create'); # lib/MyApp/Users.pm # sub create{}

    More Advanced

  • Create a route to a bridge action to initialise an object
  • my $r = $self->route(); my $admin_route = $r->bridge('/users/admin/:id') ->to('users#admin');

  • http://myapp.com/users/admin/3/edit
  • $admin_route->route('/edit')->to('users#edit');

  • http://myapp.com/users/admin/3/delete
  • $admin_route->route('/delete')->to('users#delete');

  • Corresponding controller
  • package MyApp::Users; use Mojo::Base 'Mojolicious::Controller'; sub admin { my $self = shift; my $id = $self->param('id'); # from placeholder my $user = $self->db->get_user($id); # from your db $self->stash(user => $user); } sub edit { my $self = shift; my $user = $self->stash->{user}; }

    Even more advanced

  • Add authentication/authorisation steps
  • my $authenticated_user_route = $r->bridge('/users') ->over( is => 'admin' )->to('users#authenticate'); my $user_admin_route = $authenticated_user_route->bridge('/admin/:id') ->to('users#admin');

  • Mojolicious::Plugin::Authenticate
  • Mojolicious::Plugin::Authorization
  • named routes to make url generation easier
  • $user_admin_route->route('/edit') ->name('edituser')->to('users#edit'); my $edit_route = $user_admin_route ->bridge('/edit')->to('users#setup_edit'); $edit_route->get('')->name('edituser')->to('users#edit'); $edit_route->post('')->to('users#post_edit');

  • using url_for()
  • <a href="<%= url_for('edituser',id => 23) %>">edit</a> - generates {{http://myapp.com/users/admin/23/edit}}

    4. Template System

  • A bit like Mason
  • <% Perl code %>

  • but not quite
  • <%= Perl expression, replaced with XML escaped result %> <%== Perl expression, replaced with result %> <%# Comment, useful for debugging %> <%% Replaced with "<%", useful for generating templates %> % Perl code line, treated as "<% line =%>" %= Perl expression line, treated as "<%= line %>" %== Perl expression line, treated as "<%== line %>" %# Comment line, treated as "<%# line =%>" %% Replaced with "%", useful for generating templates

    5. Conclusion

  • Mojolicious is cool
  • - no external dependencies! (That's a good thing)

  • You should try it