Added: Initial import of C-P-SubReq
[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.
21
22 =head1 METHODS
23
24 =over 4 
25
26 =item subreq action, args
27
28 =item sub_request
29
30 takes the name of the action you would like to call, as well as the
31 arguments you want to pass to it.
32
33 =back 
34
35 =cut
36
37 *subreq = \&sub_request;
38
39 sub sub_request {
40     my ( $c, $action, @args ) = @_;
41     my %old_req;
42     $old_req{stash}   = $c->{stash};$c->{stash}={};
43     $old_req{content} = $c->res->output;$c->res->output(undef);
44     $old_req{args}    = $c->req->arguments;$c->req->arguments([@args]);
45     $old_req{action}  = $c->req->action;$c->req->action($action);
46     $c->dispatch();
47     my $output  = $c->res->output;
48     $c->{stash} = $old_req{stash};
49     $c->res->output($old_req{content});
50     $c->req->arguments($old_req{args});
51     $c->req->action($old_req{action});
52     return $output;
53 }
54
55 =head1 SEE ALSO
56
57 L<Catalyst>.
58
59 =head1 AUTHOR
60
61 Marcus Ramberg, C<mramberg@cpan.org>
62
63 =head1 THANK YOU
64
65 SRI, for writing the awesome Catalyst framework
66
67 =head1 COPYRIGHT
68
69 This program is free software, you can redistribute it and/or modify it under
70 the same terms as Perl itself.
71
72 =cut
73
74 1;