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