Hi Jan,

Thanks for your reply. Don’t you think that passing app->db to every model is 
not a good idea neither because model must know itself where data are and how 
to fetch it and pass to controller? Also it’s simply inconvenient to always 
have one (or even 2 - app->db and app->redis for example) arguments passed to 
every model, no?

Cheers
Eugene

On 20 Apr 2015, at 10:07, Jan Henning Thorsen <jan.henn...@thorsen.pm> wrote:

> You don't. Passing $app or $c to a model is not a good idea. Reason for this 
> is that it makes it hard to reuse the models elsewhere. What you can do, is 
> passing data from $app when you construct your models, but I would strongly 
> advice against passing $app.
> 
> 
> On Friday, April 17, 2015 at 4:38:06 PM UTC+2, Eugene Toropov wrote:
> Greetings,
> 
> In the following example how will you make MyApp::Model::Users have access to 
> app object (which is basically $self) that is necessary to use app->db and 
> app->config?
> 
> http://mojolicio.us/perldoc/Mojolicious/Guides/Growing#WELL-STRUCTURED-APPLICATION
> 
> package MyApp;
> use Mojo::Base 'Mojolicious';
> 
> use MyApp::Model::Users;
> 
> sub startup {
>   my $self = shift;
> 
>   $self->secrets(['Mojolicious rocks']);
>   $self->helper(users => sub { state $users = MyApp::Model::Users->new });
> 
>   my $r = $self->routes;
> 
>   $r->any('/' => sub {
>     my $c = shift;
> 
>     my $user = $c->param('user') || '';
>     my $pass = $c->param('pass') || '';
>     return $c->render unless $c->users->check($user, $pass);
> 
>     $c->session(user => $user);
>     $c->flash(message => 'Thanks for logging in.');
>     $c->redirect_to('protected');
>   } => 'index');
> 
>   my $logged_in = $r->under(sub {
>     my $c = shift;
>     return 1 if $c->session('user');
>     $c->redirect_to('index');
>     return undef;
>   });
>   $logged_in->get('/protected');
> 
>   $r->get('/logout' => sub {
>     my $c = shift;
>     $c->session(expires => 1);
>     $c->redirect_to('index');
>   });
> }
> 
> 1;
> Cheers
> Eugene
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to mojolicious+unsubscr...@googlegroups.com.
> To post to this group, send email to mojolicious@googlegroups.com.
> Visit this group at http://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to