[Catalyst] forwarding to chained actions

Roberto Henríquez roberto at freekeylabs.com
Tue Apr 24 09:45:33 GMT 2007


Bernhard Graf escribió:
> Is there a way to forward to (the end point of) a chained action and run 
> the whole action chain again?
>
> Simplified example:
>
> sub instance : PathPart('') Chained('/') CaptureArgs(1) {
>   my ($self, $c, $data_id) = @_;
>   # fetch data for $data_id and put on stash
>   $c->stash(data => $self->fetch_data_for_id($data_id));
> }
>
> # view data
> # /data/<DATA_ID>
> sub view : PathPart('') Chained('instance') Args(0) {
>   my ($self, $c) = @_;
>   $c->stash(template => 'view.tt');
> }
>
> # update data from form
> # /data/<DATA_ID>/update
> sub update : PathPart Chained('instance') Args(0) {
>   my ($self, $c) = @_;
>   my $data = $c->stash->{data};
>   $self->update_from_form($data);
>   $c->forward('view'); # view data
> }
>
> update() updates the data in the database but $c->stash->{data} is not 
> changed. Then forward('view') calls view(), but this view's the 
> unchanged (outdated) data from the stash.
>
> Of course I could do a $c->stash(data => $data) before 
> $c->forward('view'), but as stated this is only a simple example.
> In many cases it would be convenient to have something like
> $c->forward_chain('view') to execute all methods of the chain.
>
> Does such a thing exist in Catalyst?
>   
Hi,

if you end your update action with a forward, you also have another 
problem. The user ends up viewing the data item under a 
"/data/xxxx/update" URL; I consider way more elegant to issue a 
redirection to the "view data" uri right after the update. Thus, in your 
case I would do

$c->res->redirect( $c->uri_for( "/data/$id") );

after the update is done. And the address displayed in the browser would 
be "/data/xxx" instead of an ugly "update" url.

Hope this helps,

--R




More information about the Catalyst mailing list