[Catalyst] Redispatching actions
Matt S Trout
dbix-class at trout.me.uk
Sun May 20 14:43:27 GMT 2007
On Sun, May 20, 2007 at 01:55:41PM +0100, Mark Zealey wrote:
> 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?
A native feature to do this will be in 5.80.
However, in your case you -do- want to redirect anyway - it means the
resultant URL for what the user sees will be correct so they can bookmark it
and/or mail it to somebody else etc. etc.
I worked on some Maypole apps a few years back that -didn't- redirect and
behaved the way you're looking at implementing and the "wrong URL" problem
turned out to be a usability nightmare - I'd strongly, strongly recommend
against it, sending a 302 really isn't that bad.
--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director Want a managed development or deployment platform?
Shadowcat Systems Ltd. Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/ http://www.shadowcatsystems.co.uk/
More information about the Catalyst
mailing list