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) = @_;
      $c->stash(cd =>
   $c->model('MyApp')->resultset('CD')->find({id=>$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) = @_;
      $c->stash(track => $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) = @_;
      $c->stash(movement => $c->stash->{track}->movement($id));
   }

-Sir

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to