[Catalyst] proper flow control with $c->foward, in search of greater grok

Tomas Doran bobtfish at bobtfish.net
Fri Jan 8 16:35:28 GMT 2010


Bill Moseley wrote:
> 
> 
> On Fri, Jan 8, 2010 at 6:55 AM, Tomas Doran <bobtfish at bobtfish.net 
> <mailto:bobtfish at bobtfish.net>> wrote:
> 
> 
> 
>         $c->response->redirect($c->uri_for("/user/$user_id/blog/$blog_id/entry/list"));
> 
> 
>     Eww, don't do that.
> 
>     You want
>     $c->response->redirect($c->uri_for_action('/user/blog/entry/list',
>     [$user_id, $blog_id]));
> 
> 
> I agree that's the right approach, but not sure I see why the "Eww" in 
> this specific case.  Can you explain?

Yes, c->uri_for("/user/$user_id/blog/$blog_id/entry/list") is hard 
coding the URI, rather than asking the dispatcher to construct it for you.

This inevitably means that you'll have to change the code when you 
refactor / change the URI layout later. Hard coding the URI paths to 
your controllers in multiple controllers / templates is also of course 
repeating yourself more than you need to be doing.

In fact, if possible, you probably want to be saying 
$c->uri_for($self->action_for('foo'), \@caps, @args);

because then you can reuse the controller code at a totally different 
URI path without having to change any code at all.

Cheers
t0m



More information about the Catalyst mailing list