[Catalyst] Chained actions and uri_for

John Napiorkowski jjn1056 at yahoo.com
Tue Nov 14 15:04:22 GMT 2006


--- Renaud <renaud at linuxaddicts.com> wrote:

> Hello,
> 
> I understood that using the Chained actions way
> would be the more proper
> way to design an "uri-based" multilangual
> application. In my case, I have
> to extract a province and a language from my url, so
> here is how I coded
> it:
> 
> In Root.pm:
> 
> sub province : Chained('/') :CaptureArgs(1)
> :PathPart('') { ... }
> sub lang : Chained('province') :CaptureArgs(1)
> :PathPart('') { ... }
> 
> In other controllers, e.g Foo.pm:
> sub foo : Chained('/lang') : Args(0) { ... }

$c->uri_for($c->controller('Foo')->action_for('foo'),
[1,2]);

> 
> That will be triggered with /*/*/foo
> 
> The problem is that I am not sure how to properly
> use uri_for with that.
> What I'd like is to have something like
> uri_for('/foo') returning
> http://whatever.com/qc/fr/foo if $province eq 'qc'
> and $lang eq 'fr'.
> 
> What I am doing so far is concatenating
> uri_for('/'), $province and
> $language into one "root" variable and using
> href="[% root %]/foo" in my
> templates. There must be a more "catalystic" way to
> do that, but I can't
> figure it out. Would anyone have a clue for me?
> 
> Actually, is there anything written about the best
> practices to code a
> multilanguage catalyst application? There are so
> many ways to do it
> (Chained, prepare_path,...), I'm quite sure it would
> help a lot of people,
> including me :)

Hi,

This took me some time to figure out as well. 
Basically you can get the URI of the endpoint of a
chain (ie, an action with Args(x) not CaptureArgs(x)
with something like

$c->uri_for($c->controller(xxx)->action_for(xxx),
[@args]);

that arrayref of @args is VERY IMPORTANT, if you give
too many or too few args it will return undef.

If you are in the same controller as the action you
want you can jump directly to:

$c->uri_for($self->action_for(xxx), [@args]);

You use the action name (the subroutine name) as the
arg for action_for.

This way you never have to deal with paths, as long as
you know the controller, action name and number of
needed args you can get the uri to anything.

So for your chain above:

$c->uri_for($c->controller('Foo')->action_for('foo'),
[1,2]);


I think would do it, although if you are in Foo.pm you
can save the typing with

$c->uri_for($self->action_for('foo'), [1,2]);

Now of course it's up to you to make sure the args
make sense in the context.  Usually I detach to an
error page if an action in a chain get's the wrong
arg, that way you stop processing the rest of the
chain.

--john



> 
> Thanks,
> Renaud
> 
> 
> _______________________________________________
> 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/
> 



 
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com



More information about the Catalyst mailing list