[Catalyst] Re: Overriding chained methods.

Bill Moseley moseley at hank.org
Thu Feb 16 01:26:41 GMT 2012


On Thu, Feb 16, 2012 at 7:40 AM, Bill Moseley <moseley at hank.org> 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 =3D> 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 =3D> sub {
    my $class =3D shift;
    my ( $name ) =3D $class =3D~ /::([^:]+)$/;

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

    return;
};




-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20120216/ead5e=
c54/attachment.htm


More information about the Catalyst mailing list