[Catalyst] RESTy Chained actions

will at serensoft.com will at serensoft.com
Fri Jun 25 15:57:59 GMT 2010


Lemme take a stab at this -- we're pretty new to Catalyst, so beware :)

I think it would work like this:

sub car_instance : Chained PathPath('car') CaptureArgs(1) {}
sub car : Chained('car_instance') Args(0) {}

sub model_instance : Chained('car_instance') PathPart('') CaptureArgs(1) {}
sub model : Chained('model_instance') PathPath('') Args(0) {}

etc.

I think. Right?

Here's a post from Aristotle a few weeks back that goes into more detail:

=3D=3D=3D=3D=3D


---------- Forwarded message ----------
From: Aristotle Pagaltzis <pagaltzis at gmx.de>
Date: Sat, May 29, 2010 at 9:10 PM
Subject: [Catalyst] Re: A point of confusion/frustration on chained actions
To: catalyst at lists.scsys.co.uk


* Ash Berlin <ash_cpan at firemirror.com> [2010-05-20 00:40]:
> sub tutorials : Chained('/') PathPart CaptureArgs(0) {
>  # stash tutorials rs
> }
> sub all_tutorials : Chained('tutorials') PathPart('') Args(0) {
>  # empty, or set stash for view
> }
>
> sub a_tutorial : Chained('tutorials') PathPart('') CaptureArgs(1) {
>  # stash tutorial
> }
> sub show_tutorial : Chained('a_tutorial') Args(0) PathPart('') {
> # setup view
> }
>
> sub comments : Chained('a_tutorial') CaptureArgs(0) PathPart {
>  # stash comments rs from stash->{tutorial}
> }
>
> # You get the idea hopefully....
> sub show_tutorial_comments : Chained('comments') Args(0) PathPart('') {}
> sub a_comment : Chained('comments') CaptureArgs(1) PathPart('') {}
>
> sub show_comment : Chained('a_comment') Args(0) PathPart('') { }
>
> sub replies : Chained('a_comment') PathPart CaptureArgs(0) {}
> sub show_replies : Chained('replies') Args(0) PathPart('') {}
> sub a_reply : Chained('replies') CaptureArgs(1) PathPart('') {}
> sub show_reply : Chained('a_reply') Args(0) PathPart('') {}

FWIW, I=92d pull this out to several controllers and then use the
naming convention I=92ve adopted for this sort of thing:

   package MyApp::Controller::Tutorials;
   sub base : Chained PathPart('tutorials') CaptureArgs(0) {
       # stash tutorials rs
   }
   sub list : Chained('base') PathPart('') Args(0) {
       # setup index view
   }
   sub item : Chained('base') PathPart('') CaptureArgs(1) {
       # stash one tutorial
   }
   sub view : Chained('item') PathPart('') Args(0) {
       # almost always empty
   }

   package MyApp::Controller::Comments;
   sub base : Chained('/tutorials/item') PathPart('comments') CaptureArgs(0)
{
        # stash comments rs from stash->{tutorial}
   }
    sub list : Chained('base') PathPart('') Args(0) {}
   sub item : Chained('base') PathPart('') CaptureArgs(1) {}
   sub view : Chained('item') PathPart('') Args(0) {}


   package MyApp::Controller::Replies;
   sub base : Chained('/comments/item') PathPart('comments') CaptureArgs(0)
{}
   sub list : Chained('base') PathPart('') Args(0) {}
   sub item : Chained('base') PathPart('') CaptureArgs(1) {}
   sub view : Chained('item') PathPart('') Args(0) {}

You get the idea.

This also makes it very nice to navigate the template structure
when using RenderView, as you can always relate everything to its
purpose at a glance without referring to the controllers once.

I kept the namespace structure flat here intentionally.
I recommend doing so even if your URIs are nested =96 unless there
are several different kinds of comments and replies in the
system. In that case I would nest the package names in order to
make it simpler to navigate the source. Chained dispatch makes it
easy to get just about any URI structure, regardless of how the
code is laid out; this is a feature, you should use it.
=3D=3D=3D=3D=3D


On Fri, Jun 25, 2010 at 10:02 AM, John Lifsey - Contractor - 5595 <
john.lifsey at nrl.navy.mil> wrote:

> Is there any way to chain actions such that they can be endpoints or
> intermediate steps?
>
> For example:
>
> I want to have a REST controller that allows these URLs to work using
> ActionClass REST and Chained
>
> http://myapp.com/car - dispatch to sub car_GET for list of cars
>
> http://myapp.com/car/corvette - dispatch to car_GET for a single record
>
> http://myapp.com/car/corvette/model - dispatch to sub model_GET chained to
> sub car{} for a list of corvette types
>
> http://myapp.com/car/corvette/model/Z06 - dispatch to sub model_GET
> chained to sub car{} for a single corvette type record
>
> something like this doesn't quite work:
>
> sub car :ActionClass('REST') Chained CaptureArgs(1) {}
> sub model :ActionClass('REST') Chained('car') Args(1) {}
>
> So my question: Is there a Chained incantation to allow all of the
> following to be endpoints?
>
> /car
> /car/aCarID
> /car/aCarID/model
> /car/aCarID/model/aModelID
>
> Or am I just DoingItWrong all together?
>
> _______________________________________________
> 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/
>



-- =

will trillich
"I think it would be worse to expect nothing than to be disappointed." --
Anne (with an 'e') Shirley
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100625/1805e=
e80/attachment.htm


More information about the Catalyst mailing list