Clear questions and runnable code
get the best and fastest answer
Catalyst: Using Chained for URL Dispatching and Sane Method Organizationby dhoss (Vicar)
|on Oct 17, 2009 at 22:27 UTC||Need Help??|
I wrote this for http://catalyzed.org. Please review and let me know if it stands worthy of posting in tutorials.
You've read the tutorial but you still can't seem to get a grasp on what exactly to use Chained dispatching for.
Chained is just another method of method/url dispatching that Catalyst allows you to use. Chained, however, allows you to chain together methods that need to share information across requests....
Say you want to construct a url that looks something like this: http://www.yourhost.com/item/1/view. Normally, you'd have to go through a bunch of parsing involving
Let's break this method down, and then we'll get to the rest of this chain.
the Chained('/') attribute sets up the base of the chain.
PathPart determines the name of the path to capture.
CaptureArgs tells Chained to capture one argument directly after the path part.
Now, let's finish the chain so we can have a working URL.
This method is fairly self explanatory, but just to make sure, we'll go ahead and go over it.
As in the comments, Chained is passed the name of the base point of the chain, which in this case is 'base'. We determine the path part with PathPart once again, which can be noted that it doesn't have to match the current method's name. The one different attribute is the Args(0) attribute. Args tells Chained that the end of the chain has been reached, thus giving you a usable URL at this point. You can specify arguments to be passed in Args(), and they can be accessed via the @_ variable, like $self and $c.
To recap, this gives us a url like http://www.yourdomain.com/item/itemid/view. You can expand this for CRUD actions so you can have URLs like the above, as well as something like /item/create. Here is some example code:
This actually creates a couple different Chained instances, giving us the ability to have urls like /blog/id/view, blog/new, etc.
Next time, using $c->urlforaction to create URIs for Chained methods.