Skip to content

Commit

Permalink
use MyApp::Model namespace in growing guide
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 6, 2014
1 parent 5bbde73 commit 111e1b6
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/Mojolicious/Guides/Growing.pod
Expand Up @@ -206,14 +206,16 @@ In L<Mojolicious> we consider web applications simple frontends for existing
business logic, that means L<Mojolicious> is by design entirely L<model> layer
agnostic and you just use whatever Perl modules you like most.

$ mkdir -p lib/MyApp
$ touch lib/MyApp/Users.pm
$ chmod 644 lib/MyApp/Users.pm
$ mkdir -p lib/MyApp/Model
$ touch lib/MyApp/Model/Users.pm
$ chmod 644 lib/MyApp/Model/Users.pm

Our login manager will simply use a plain old Perl module abstracting away all
logic related to matching usernames and passwords.
logic related to matching usernames and passwords. The name
C<MyApp::Model::Users> is an arbitrary choice, and is simply used to make the
separation of concerns more visible.

package MyApp::Users;
package MyApp::Model::Users;

use strict;
use warnings;
Expand Down Expand Up @@ -246,10 +248,10 @@ templates.
use Mojolicious::Lite;

use lib 'lib';
use MyApp::Users;
use MyApp::Model::Users;

# Helper to lazy initialize and store our model object
helper users => sub { state $users = MyApp::Users->new };
helper users => sub { state $users = MyApp::Model::Users->new };

# /?user=sri&pass=secr3t
any '/' => sub {
Expand Down Expand Up @@ -394,12 +396,12 @@ this.
use Mojolicious::Lite;

use lib 'lib';
use MyApp::Users;
use MyApp::Model::Users;

# Make signed cookies secure
app->secrets(['Mojolicious rocks']);

helper users => sub { state $users = MyApp::Users->new };
helper users => sub { state $users = MyApp::Model::Users->new };

# Main login action
any '/' => sub {
Expand Down Expand Up @@ -515,13 +517,13 @@ actual action code needs to be changed.
package MyApp;
use Mojo::Base 'Mojolicious';

use MyApp::Users;
use MyApp::Model::Users;

sub startup {
my $self = shift;

$self->secrets(['Mojolicious rocks']);
$self->helper(users => sub { state $users = MyApp::Users->new });
$self->helper(users => sub { state $users = MyApp::Model::Users->new });

my $r = $self->routes;

Expand Down Expand Up @@ -627,13 +629,13 @@ information.
package MyApp;
use Mojo::Base 'Mojolicious';

use MyApp::Users;
use MyApp::Model::Users;

sub startup {
my $self = shift;

$self->secrets(['Mojolicious rocks']);
$self->helper(users => sub { state $users = MyApp::Users->new });
$self->helper(users => sub { state $users = MyApp::Model::Users->new });

my $r = $self->routes;
$r->any('/')->to('login#index')->name('index');
Expand Down

0 comments on commit 111e1b6

Please sign in to comment.