released 0.8
[catagits/Catalyst-Plugin-SubRequest.git] / SubRequest.pm
index d8f5b20..609a3a8 100644 (file)
@@ -2,7 +2,7 @@ package Catalyst::Plugin::SubRequest;
 
 use strict;
 
-our $VERSION = '0.04';
+our $VERSION = '0.08';
 
 
 =head1 NAME
@@ -13,22 +13,23 @@ Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst
 
     use Catalyst 'SubRequest';
 
-    $c->subreq('!test','foo','bar');
+    $c->subreq('/test/foo/bar', { template => 'magic.tt' });
 
 =head1 DESCRIPTION
 
-Make subrequests to actions in Catalyst.
+Make subrequests to actions in Catalyst. Uses the  catalyst
+dispatcher, so it will work like an external url call.
 
 =head1 METHODS
 
 =over 4 
 
-=item subreq action, args
+=item subreq path, [stash as hash ref], [parameters as hash ref]
 
 =item sub_request
 
-takes the name of the action you would like to call, as well as the
-arguments you want to pass to it.
+Takes a full path to a path you'd like to dispatch to. Any additional
+parameters are put into the stash.
 
 =back 
 
@@ -36,19 +37,29 @@ arguments you want to pass to it.
 
 *subreq = \&sub_request;
 
+use Data::Dumper qw/Dumper/;
 sub sub_request {
-    my ( $c, $action, @args ) = @_;
+    my ( $c, $path, $stash, $params ) = @_;
+
     my %old_req;
-    $old_req{stash}   = $c->{stash};$c->{stash}={};
+    $path =~ s/^\///;
+    local $c->{stash}=$stash || {};
     $old_req{content} = $c->res->output;$c->res->output(undef);
-    $old_req{args}    = $c->req->arguments;$c->req->arguments([@args]);
-    $old_req{action}  = $c->req->action;$c->req->action($action);
+    $old_req{args}    = $c->req->arguments;
+    $old_req{action}  = $c->req->action;$c->req->action(undef);
+    $old_req{path}    = $c->req->path;$c->req->path($path);
+    $old_req{params}  = $c->req->params;$c->req->params($params || {});
+    $c->prepare_action();
+    $c->log->debug("Subrequest to $path , action is ".  $c->req->action )
+        if $c->debug;
+    # FIXME: Hack until proper patch in NEXT.
+    local $NEXT::NEXT{$c,'dispatch'};
     $c->dispatch();
     my $output  = $c->res->output;
-    $c->{stash} = $old_req{stash};
-    $c->res->output($old_req{content});
+    $c->req->{params}=$old_req{params};
     $c->req->arguments($old_req{args});
-    $c->req->action($old_req{action});
+    $c->req->path($old_req{path});
+    $c->res->output($old_req{content});
     return $output;
 }