[Catalyst] Redispatching actions

Mark Zealey mark at itsolve.co.uk
Sun May 20 13:55:41 GMT 2007


Hi there,

I was wondering if there was some way within the Catalyst api to redispatch 
calls. The problem I'm having is:

package MyApp::C::Blah;

sub add : Local :Form(...) {
  if($form->submitted) {
    # Add to database
    $c->res->redirect($c->uri_for('view'))
  }
  # Generate form and display
}
sub edit : Local { ... same as add but fill out form initially }
sub delete: Local {
  # Delete item
    $c->res->redirect($c->uri_for('view'))
}
sub view : Local {
  # display page
}
sub index : Private {
    $c->res->redirect($c->uri_for('view'))
}


This is an awful lot of duplicated code, forever looking up the uri for view 
and then redirecting there when i want to send the user to it. I could do 
$c->forward('view'), which would reduce the overhead, and stop me having to 
send so many 302 responses etc, but because I'm using TT, $c->action->name is 
automatically used as the template file to process, which is what I want. I 
could do $c->stash->{template} = '/blah/view.tt' in view(), but that's 
annoying as TT already does that for me automatically. There must be some way 
to simply say "I want to redispatch to the view() action" in such a way as 
$c->action->name is set to view, etc. I guess Catalyst::Plugin::SubRequest 
would do that, but again that's quite a lot of overhead and I don't really 
want all the request data to be localized... Anyone got any ideas or should I 
write a $c->redispatch plugin?

Thanks,

Mark



More information about the Catalyst mailing list