[Catalyst] Per request data in controller base class
Sir Robert Burbridge
rburbrid at cisco.com
Wed Nov 24 17:55:13 GMT 2010
On 11/23/2010 09:12 PM, Bill Moseley wrote:
> 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';
> 1;
>
> 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.
>
Suggeston: You already have the information encoded into the URI in a =
guaranteed unique way; keep the same convention for the stash.
package MyApp::WUI::Controller::CD;
use Moose;
use namespace::autoclean;
BEGIN {extends 'Catalyst::Controller'}
sub index :Chained('/') :PathPart('cd') :CaptureArgs(1) {
my ($self, $c, $id) =3D @_;
$c->stash(cd =3D>
$c->model('MyApp')->resultset('CD')->find({id=3D>$id}));
}
package MyApp::WUI::Controller::CD::Track;
use Moose;
use namespace::autoclean;
BEGIN {extends 'Catalyst::Controller'}
sub index :ChainedParent :PathPart('track') :CaptureArgs(1) {
my ($self, $c, $id) =3D @_;
$c->stash(track =3D> $c->stash->{$cd}->track($id));
}
package MyApp::WUI::Controller::CD::Track::Movement;
use Moose;
use namespace::autoclean;
BEGIN {extends 'Catalyst::Controller'}
sub index :ChainedParent :PathPart('movement') :CaptureArgs(1) {
my ($self, $c, $id) =3D @_;
$c->stash(movement =3D> $c->stash->{track}->movement($id));
}
-Sir
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20101124/af3ba=
72f/attachment.htm
More information about the Catalyst
mailing list