[Catalyst] Per request data in controller base class

Bill Moseley moseley at hank.org
Wed Nov 24 02:12:10 GMT 2010

I have a controller base class that needs to stash some data per request.
 My question is where to put that data (stash would seem best) -- and how to
name it.

This class is an auto-CRUD-like controller base class that does the mundane
work of fetching objects.  For example:

package MyApp::Controller::CD;
use parent 'My::Controller::CRUD';

Which then will provided an action that handles "GET /cd/$id" which will
fetch the CD object and place it someplace.  For example, I could just put
it in $c->stash->{item}.

Now, controllers can be chained together, so for example I might have a
chain /cd/*/track/*/movement/* which all use the same base class, and where
in the "movement" action I might want to be able to fetch the cd and track
objects fetched when processing the chain.  So, $c->stash->{item} isn't such
a good name.

One option might be for the base class to use its class name as a namespace
in the stash.  That is, $c->stash->{CD}->{item} and

Another option is to add attributes to the base class so I can then do:
 $c->controller( 'CD' )->item and $c->controller( 'Track' )->item;  But, I'd
need to be sure and clear those at the start of every request since this is
per-request data, unlike other attributes of the controller instance.

# Run before the root of chain action is run.
before 'base' =3D> sub {
    my ( $self, $c ) =3D @_;
    $self->$_ for qw/ clear_item clear_item_id clear_resultset /;

Although that depends on the chain running to clear out the previous values.
 So, the stash is probably safer.

Of course, could still add methods of the same name $c->controller( 'CD'
)->item that is a wrapper for saving to or reading from the stash, although
that means $c would have to be available to the Controller.

So, where should this per-request data get saved, and what do you recommend
for naming?

BTW -- Catalyst::Component::ACCEPT_CONTEXT says "Make the current Catalyst
request context available in *Models and Views*".  Any reason not to use
this for Controllers?

-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20101123/c71a6=

More information about the Catalyst mailing list