[Catalyst] Accessing $c from Model

John Napiorkowski jjn1056 at yahoo.com
Wed Dec 27 20:54:29 GMT 2006

--- Eden Cardim <edencardim at gmail.com> wrote:

> 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?
> What's wrong with $c->forward('Model::Log', 'info',
> ['foo']) ?

This discussion is bring back old nightmares for me :)
 Catalyst is really flexible but I personally haven't
really figured out the value of:

## Forward style
$c->forward('Model::Log', 'info', ['foo'])


## direct invocation style
$c->model('Log')->info("foo") with/without using
ACCEPT_CONTEXT to have this instantiate a new object
for each request


## Classic Perl style
Use Model::Log;

my $log = Model::Log->new(...);

I tend to use forwarding mostly for controllers,
although with Chaining I find I need this less and
less.  I also use forward to delegate to a view

>From what I can see the main advantage of forward is
that it sends the context for you automatically.  Also
you get the automatic return but this to me seems less
important with a model and more relevant to
controllers.  Usually my models return self or some
sort of value anyway.

The only advantage I can see for making your business
objects into catalyst models is that you get the handy
config methods that you can centralize in your main
configuration file if you like.  And it saves you
having to add 'use ...' at the top of all the
controllers that need a particular model (although you
can create a base controller to help with this issue).

I'm certainly offtopic by now but this question gets
asked of me by my coworkers and I wish I had more than
a list of pro/cons to give as an answer.


> -- 
> Eden Cardim
> Instituto Baiano de Biotecnologia
> Núcleo de Biologia Computacional e Gestão de
> Informações Biotecnológicas
> Laboratório de Bioinformática
> --
> "you seem to think that 'close enough' is close
> enough...
> please learn to be 'literal' around programming."
> merlyn - on irc.freenode.net#perl
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo:
> http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive:
> Dev site: http://dev.catalyst.perl.org/

Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Catalyst mailing list