[Catalyst] RESTy Chained actions
Stephen Howard
stephen at enterity.com
Tue Jun 29 13:43:42 GMT 2010
I may be off the mark on what you're trying to do, but here goes.
In my brief experience with catalyst, inheritance doesn't do much for
you when constructing actions.
I believe that chained actions are pretty tightly bound to the urls
you are building, so there's no way to reuse them for different urls.
That said, something I often do is $c->forward my way to common
private functions, rather than try to do it through inheritance. So,
from that same project:
sub find_tutorial :Chained('/') PathPart('tutorials') CaptureArgs(1) {
$_[1]->forward('/exists',['tutorial' => 'Tutorial' => $_[2] ])
}
sub find_comment :Chained('find_tutorial') PathPart('comments')
CaptureArgs(1) {
$_[1]->forward('/exists',['comment' => 'Comment::Thread' =>
$_[2] ])
}
where, 'exists' is defined in my root controller as:
sub exists :Private {
my( $self, $c, $name, $class, $id ) = @_;
unless( $c->stash->{$name} = $c->model("DB::$class")->find($id) ) {
$c->res->status('404');
$c->detach('/default');
}
}
HTH
Stephen
On Jun 29, 2010, at 6:06 AM, John Lifsey - Contractor - 5595 wrote:
>
> On 6/25/10 12:08 PM, Stephen Howard wrote:
>> /tutorials
>> /tutorials/*
>> /tutorials/*/comments
>> /tutorials/*/comments/*
>> /tutorials/*/comments/*/replies
>> /tutorials/*/comments/*/replies/*
>>
>> #
>> ------------------------------------------------------------------------------
>>
>> sub list :Chained('/') PathPart('tutorials') Args(0) :
>> ActionClass('REST') {}
>>
>> sub list_GET { ... }
>> sub list_POST { ... }
>> sub list_HEAD { ... }
>>
>> #
>> ------------------------------------------------------------------------------
>>
>> sub find_tutorial :Chained('/') PathPart('tutorials')
>> CaptureArgs(1) {
>> ... }
>>
>> #
>> ------------------------------------------------------------------------------
>>
>> sub single :Chained('find_tutorial') PathPart('') Args(0)
>> ActionClass('REST') { ... }
>> sub single_GET { ... }
>> sub single_POST { ... }
>>
>> #
>> ------------------------------------------------------------------------------
>>
>> sub comments :Chained('find_tutorial') PathPart('comments') Args(0)
>
> Well, if you teach a man to fish, apparently he'll ask you to teach
> him how to fly spaceships next.
>
> While this example worked well, it led me to another related
> problem. I have a parent Controller class for typical behavior on a
> resource from which I inherit and override as needed for each type
> of resource.
>
> package MyBaseController;
> extends 'Catalyst::Controller::REST';
>
> sub index : Chained PathPrefix Args(0) ActionClass('REST') {}
> sub find : Chained PathPrefix CaptureArgs(1) {}
> sub single : Chained('find') PathPart('') Args(0) {}
> etc..
>
> 1;
>
> Using the previous poster's example this works just fine in
> MyApp::Controller::Tutorial
>
> but it will not work for MyApp::Controller::Tutorial::Comments
> At first, from looking at the documentation I thought that setting
> the controller config key "path" to a regex or "/tutorial/*/" might
> do the trick, but alas it does not.
>
> This is further complicated by my desire to break from the previous
> example and allow Comments to be at the same hierarchical level as
> Tutorial. That is to say:
>
> MyApp::Controller::Tutorial ISA MyBaseController;
> and
> MyApp::Controller::Comments ISA MyBaseController;
>
> So that I could reference a particular comment ID at /myapp/comments/
> * or at /myapp/tutorial/*/comments/*
>
> Upon documentation review I thought __PACKAGE__-
> >config(action=>{'*'=>{ blah, blah }}); would be my salvation, but
> it appears that '*' doesn't actually work in myapp, at least as
> written in the documentation.
>
> Now that I've gotten myself worked entirely into a lather thinking
> in circles it's time to request insight from the crowd. Any
> suggestions?
>
>
> _______________________________________________
> List: Catalyst at 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/
More information about the Catalyst
mailing list