updated pod for 5.0, added some debug to testapp.
[catagits/Catalyst-Plugin-SubRequest.git] / SubRequest.pm
1 package Catalyst::Plugin::SubRequest;
2
3 use strict;
4
5 our $VERSION = '0.04';
6
7
8 =head1 NAME
9
10 Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst
11
12 =head1 SYNOPSIS
13
14     use Catalyst 'SubRequest';
15
16     $c->subreq('/test','foo','bar');
17
18 =head1 DESCRIPTION
19
20 Make subrequests to actions in Catalyst. Uses the private name of
21 the action for dispatch.
22
23 =head1 METHODS
24
25 =over 4 
26
27 =item subreq action, args
28
29 =item sub_request
30
31 takes the name of the action you would like to call, as well as the
32 arguments you want to pass to it.
33
34 =back 
35
36 =cut
37
38 *subreq = \&sub_request;
39
40 sub sub_request {
41     my ( $c, $action, @args ) = @_;
42     my %old_req;
43     $old_req{stash}   = $c->{stash};$c->{stash}={};
44     $old_req{content} = $c->res->output;$c->res->output(undef);
45     $old_req{args}    = $c->req->arguments;$c->req->arguments([@args]);
46     $old_req{action}  = $c->req->action;$c->req->action($action);
47     $c->dispatch();
48     my $output  = $c->res->output;
49     $c->{stash} = $old_req{stash};
50     $c->res->output($old_req{content});
51     $c->req->arguments($old_req{args});
52     $c->req->action($old_req{action});
53     return $output;
54 }
55
56 =head1 SEE ALSO
57
58 L<Catalyst>.
59
60 =head1 AUTHOR
61
62 Marcus Ramberg, C<mramberg@cpan.org>
63
64 =head1 THANK YOU
65
66 SRI, for writing the awesome Catalyst framework
67
68 =head1 COPYRIGHT
69
70 This program is free software, you can redistribute it and/or modify it under
71 the same terms as Perl itself.
72
73 =cut
74
75 1;