14263b88616e65c57a4f0ec3c14e92200429f6a4
[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 a full path to a path you'd like to dispatch to.
32
33 =back 
34
35 =cut
36
37 *subreq = \&sub_request;
38
39 sub sub_request {
40     my ( $c, $path ) = @_;
41     my %old_req;
42     $path =~ s/^\///;
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;
46     $old_req{action}  = $c->req->action;$c->req->action(undef);
47     $old_req{path}  = $c->req->path;$c->req->path($path);
48     $c->prepare_action();
49     $c->dispatch();
50     my $output  = $c->res->output;
51     $c->{stash} = $old_req{stash};
52     $c->res->output($old_req{content});
53     $c->req->arguments($old_req{args});
54     $c->req->action($old_req{action});
55     return $output;
56 }
57
58 =head1 SEE ALSO
59
60 L<Catalyst>.
61
62 =head1 AUTHOR
63
64 Marcus Ramberg, C<mramberg@cpan.org>
65
66 =head1 THANK YOU
67
68 SRI, for writing the awesome Catalyst framework
69
70 =head1 COPYRIGHT
71
72 This program is free software, you can redistribute it and/or modify it under
73 the same terms as Perl itself.
74
75 =cut
76
77 1;