Commit | Line | Data |
aae30f91 |
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 | |
1774feac |
16 | $c->subreq('/test/foo/bar'); |
aae30f91 |
17 | |
18 | =head1 DESCRIPTION |
19 | |
2e5b4f5d |
20 | Make subrequests to actions in Catalyst. Uses the private name of |
21 | the action for dispatch. |
aae30f91 |
22 | |
23 | =head1 METHODS |
24 | |
25 | =over 4 |
26 | |
27 | =item subreq action, args |
28 | |
29 | =item sub_request |
30 | |
1774feac |
31 | Takes a full path to a path you'd like to dispatch to. |
aae30f91 |
32 | |
33 | =back |
34 | |
35 | =cut |
36 | |
37 | *subreq = \&sub_request; |
38 | |
39 | sub sub_request { |
1774feac |
40 | my ( $c, $path ) = @_; |
aae30f91 |
41 | my %old_req; |
1774feac |
42 | $path =~ s/^\///; |
aae30f91 |
43 | $old_req{stash} = $c->{stash};$c->{stash}={}; |
44 | $old_req{content} = $c->res->output;$c->res->output(undef); |
1774feac |
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(); |
9252a294 |
49 | $c->log->debug("Subrequest to $path , action is ". |
50 | $c->req->action ) |
51 | if $c->debug; |
aae30f91 |
52 | $c->dispatch(); |
53 | my $output = $c->res->output; |
54 | $c->{stash} = $old_req{stash}; |
55 | $c->res->output($old_req{content}); |
56 | $c->req->arguments($old_req{args}); |
57 | $c->req->action($old_req{action}); |
58 | return $output; |
59 | } |
60 | |
61 | =head1 SEE ALSO |
62 | |
63 | L<Catalyst>. |
64 | |
65 | =head1 AUTHOR |
66 | |
67 | Marcus Ramberg, C<mramberg@cpan.org> |
68 | |
69 | =head1 THANK YOU |
70 | |
71 | SRI, for writing the awesome Catalyst framework |
72 | |
73 | =head1 COPYRIGHT |
74 | |
75 | This program is free software, you can redistribute it and/or modify it under |
76 | the same terms as Perl itself. |
77 | |
78 | =cut |
79 | |
80 | 1; |