Allow forward with arguments.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
index 72a8ef3..af4f7ce 100644 (file)
@@ -2,6 +2,7 @@ package Catalyst::Dispatcher;
 
 use strict;
 use base 'Class::Data::Inheritable';
+use Catalyst::Exception;
 use Catalyst::Utils;
 use Text::ASCIITable;
 use Tree::Simple;
@@ -103,10 +104,13 @@ sub dispatch {
     }
 }
 
-=item $c->forward($command)
+=item $c->forward($command [args]))
 
 Forward processing to a private action or a method from a class.
 If you define a class without method it will default to process().
+also takes an optional arrayref containing arguments to be passed
+to the new function. $c->req->args will be reset upon returning 
+from the function.
 
     $c->forward('/foo');
     $c->forward('index');
@@ -126,6 +130,15 @@ sub forward {
 
     my $caller    = caller(0);
     my $namespace = '/';
+    my $args;
+
+    if ( ref( $_[0] ) eq 'ARRAY' ) {
+        $args=$c->req->args();
+        $c->req->args( shift );
+    } elsif ( ref( $_[1] ) eq 'ARRAY' ) {
+        $args=$c->req->args();
+        $c->req->args( $_[1] );
+    }
 
     if ( $command =~ /^\// ) {
         $command =~ /^\/(.*)\/(\w+)$/;
@@ -183,10 +196,12 @@ sub forward {
 
     for my $result ( @{$results} ) {
         $c->execute( @{ $result->[0] } );
-        return if scalar @{ $c->error };
+        last if scalar @{ $c->error };
         last unless $c->state;
     }
+    $c->req->args( $args ) if $args;
 
+    return if scalar @{ $c->error };
     return $c->state;
 }
 
@@ -444,7 +459,7 @@ sub setup_actions {
     };
 
     $walker->( $walker, $self->tree, '' );
-    $self->log->debug( 'Loaded private actions', $privates->draw )
+    $self->log->debug( "Loaded private actions:\n" . $privates->draw )
       if ( @{ $privates->{tbl_rows} } );
 
     my $publics = Text::ASCIITable->new;
@@ -459,7 +474,7 @@ sub setup_actions {
         $publics->addRow( "/$plain", $reverse );
     }
 
-    $self->log->debug( 'Loaded public actions', $publics->draw )
+    $self->log->debug( "Loaded public actions:\n" . $publics->draw )
       if ( @{ $publics->{tbl_rows} } );
 
     my $regexes = Text::ASCIITable->new;
@@ -474,7 +489,7 @@ sub setup_actions {
         $regexes->addRow( $regex, $reverse );
     }
 
-    $self->log->debug( 'Loaded regex actions', $regexes->draw )
+    $self->log->debug( "Loaded regex actions:\n" . $regexes->draw )
       if ( @{ $regexes->{tbl_rows} } );
 }