On Thu, Feb 16, 2012 at 7:40 AM, Bill Moseley <[email protected]> wrote:

> I have an app that is naturally hierarchical, so to make up an example a
> path might be:
>
> /version1/country/12/region/31/state/12/city/45
>
>
> which I use Chained actions to implement.
>

BTW -- On a side note (and as a sanity check) here's how I'm implementing
this.  It's a REST style app, and I need actions:

GET /country/123/region  -- "list" all regions in the country
POST /country/123/region - add to the "list" of regions in that country.

GET /country/123/region/456 - get the specific "item"
PUT, DELETE as normal.

I'm always working with a "list" or an "item" in each controller -- so
decided to just standardize on that and write less code.

Then my controllers mostly look like this:

package App::Controller::Country::Region;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller::REST' }
with 'App::ChainedREST';


          sub list_GET {}  #  /country/$id/region

          sub item_GET {}  # /country/$id/region/$id

          before args => sub { ... ]  # or whatever.

And so on.

My poorly-named "ChainedREST' role looks mostly like this:

sub base : Chained( '../arg' ) CaptureArgs(0) { }
sub list : Chained( 'base' ) PathPart( '' ) : Args(0) ActionClass(REST) { }

sub arg : Chained( 'base' ) PathPart( '') CaptureArgs(1) {}
sub item : Chained( 'arg' ) PathPart( '' ) : Args(0) ActionClass(REST) { }




Means I must add an "args" action to my Root controller.  And I also have a
BUILDARGS method to set the PathPart based on the
controller's name:

before BUILDARGS => sub {
    my $class = shift;
    my ( $name ) = $class =~ /::([^:]+)$/;

    $class->config->{action}{base}{PathPart} ||= lc $name;

    return;
};




-- 
Bill Moseley
[email protected]
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to