[Catalyst] Questions about chained actions, endpoints, etc.

Danny Warren danny at io.com
Wed Mar 21 22:56:06 GMT 2007


Now try combining all that with using Chained('.') to spread the chain 
across controllers!  (Which is really damned cool, and I use it to split 
out my model relations in to chained controllers and actions with and 
get clean uris).

Hint on how to do this (since I see that come up a lot on the list as well).


In Catalyst::Controller::Admin...

# See previous example for index, add, view, edit and delete actions 
related to the "Admin" controller

# Add a CaptureArgs method to hook our chain up to the child Admin::Foo
# subcontroller
# NOTE: I am sure you could do some more interesting things here, but I
# prefer to keep all the model interactions and stuff in the real
# controller and just use this as a hook
sub foo : Chained('admin') CaptureArgs(0)
{
   my ( $self, $c ) = @_;
}


In Catalyst::Controller::Admin::Foo...

# Chain endpoint for adding a related foo to a book
# Responds to /admin/[id]/foo/add
sub add : Chained('.') Args(0) { ... }

# Captures a foo id in the chain that is related to
# the book we already have in the chain from earlier
sub get_foo : PathPart('') Chained('.') CaptureArgs(1)
{
   my ( $self, $c, $id) = @_;
   ... # Grab the book from the stash, use it to get the related foo
       # and put THAT in the stash, etc
}

# Chain endpoint for viewing a foo related to a book
# Responds to /admin/[id]/foo/[id]/
sub display : PathPart('') Chained('get_foo') Args(0) { ... }

# Chain endpoint for editing a foo related to a book
# Responds to /admin/[id]/foo/[id]/edit
sub edit : Chained('get_foo') Args(0) { ... }

# Chain endpoint for deleting a foo related to a book
# Responds to /admin/[id]/foo/[id]/delete
sub delete : Chained('get_foo') Args(0) { ... }


Keeps your relations, controllers and uri lyaout nice and clean.

I have seen discussion here previously that Chained('.') is a confusing 
way to express what is going on - but once you wrap your head around the 
"Chained() in a Args method is for referring to CaptureArgs methods not 
uri paths" thing, it starts to make a lot more sense and feels natural.

Hope all this was interesting to *somebody* ;)

Danny Warren

Doran L. Barton wrote:
> Danny, 
> 
> Thanks for your reply. I finally had that epiphany right before your
> message came through.
> 
> The part I was not understanding was that the argument to the Chained()
> action is the private path name:
> 
>     # Handles /admin/*
>     sub book_detail : PathPart('admin') Chained('/') Args(1) {...}
> 
>     # Handler 1 of 2 for /admin/*/{edit,delete}
>     sub book_link : PathPart('admin') Chained('/') CaptureArgs(1) {...}
> 
>     sub edit : PathPart Chained('book_detail') Args(0) {...}
> 
> I wasn't grasping that "minor detail" that the edit() function needed to be 
> 
>     sub edit : PathPart Chained('admin') Args(0) {...}
> 
> Thanks! Now on to the next brick wall. :)
> 
> -=Fozz
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/



More information about the Catalyst mailing list