[Catalyst] Accessing $c from Model

John Napiorkowski jjn1056 at yahoo.com
Wed Dec 27 15:02:20 GMT 2006


--- Lorn <lorn.br at gmail.com> wrote:

> you can passa the $c from controller to model
> $c->model(foo)->bar($c,$foobar);

I'd vote for this method over setting the context,
since one of the goals of a model is to not care about
how or when it's called, that way you are encouraging
yourself to not build a fragile application where
everything is interdependent.  Actually I'd take it a
step further and only pass the stuff from the context
that you need, like $c->request if you need the
request object, etc.

That being said I have used the method suggested by
another responder for grabbing the context, mostly so
that I can do debugging inside the model
($c->log->...).  If there was better way to access the
log object from views and models that don't require
getting the context or getting $c->log than I am sure
I'd never need it.

A seemless way to share $c->log with DBIx::Class
objects would also be nice.

Also, as an aside, if you are building a custom logger
it might be more fun to create your own logger object
so that you can do $c->log->mylogger(...), or I
believe you can use the Log4Perl catalyst plugin and
configure it to log to a databse.  That way you can
skip the whole creating a custom model for this need.

--john

> 
> On 12/27/06, Mark Zealey <mark at itsolve.co.uk> wrote:
> >
> > Hi there,
> >
> > I'm basically wanting to write a simple log
> function which logs hits on my
> > website as entries in a database (automatically
> adding $c->user->{id} and
> > $c->req->referrer etc), but to do so I want to use
> a model (I think). Any
> > ideas how I can just say
> $c->model('Log')->info("foo") and automatically
> > get
> > $c passed in? I think I could have sth like:
> >
> > package MyApp::Model::Log;
> > use base 'Catalyst::Model';
> >
> > my $last_c;
> >
> > sub ACCEPT_CONTEXT {
> >         my ($self, $c) = @_;
> >         $last_c = $c;
> > }
> >
> > sub info {
> >         my ($self, $msg) = @_
> >         my $c = $last_c;
> >         ...
> > }
> >
> > but this seems pretty messy...
> >
> > Mark
> >
> > _______________________________________________
> > List: Catalyst at lists.rawmode.org
> > Listinfo:
> http://lists.rawmode.org/mailman/listinfo/catalyst
> > Searchable archive:
> >
>
http://www.mail-archive.com/catalyst@lists.rawmode.org/
> > Dev site: http://dev.catalyst.perl.org/
> >
> 
> 
> 
> -- 
> Lorn
> - Slackware Linux
> www.slackwarezine.com.br
> - http://lornlab.org
> > _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo:
> http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive:
>
http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Catalyst mailing list