Commit | Line | Data |
aae30f91 |
1 | package Catalyst::Plugin::SubRequest; |
2 | |
3 | use strict; |
4 | |
a23f7a97 |
5 | our $VERSION = '0.09'; |
aae30f91 |
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 | |
885f6da0 |
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 | |
885f6da0 |
27 | =item subreq path, [stash as hash ref], [parameters as hash ref] |
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 { |
885f6da0 |
42 | my ( $c, $path, $stash, $params ) = @_; |
8c464987 |
43 | |
1774feac |
44 | $path =~ s/^\///; |
cf71661b |
45 | local $c->{stash} = $stash || {}; |
a23f7a97 |
46 | local $c->res->{body} = undef; |
cf71661b |
47 | local $c->req->{arguments} = $c->req->{arguments}; |
48 | local $c->req->{action}; |
49 | local $c->req->{path}; |
50 | local $c->req->{params}; |
51 | |
52 | $c->req->path($path); |
53 | $c->req->params($params || {}); |
1774feac |
54 | $c->prepare_action(); |
885f6da0 |
55 | $c->log->debug("Subrequest to $path , action is ". $c->req->action ) |
56 | if $c->debug; |
c82d9501 |
57 | # FIXME: Hack until proper patch in NEXT. |
58 | local $NEXT::NEXT{$c,'dispatch'}; |
aae30f91 |
59 | $c->dispatch(); |
a23f7a97 |
60 | return $c->res->body; |
aae30f91 |
61 | } |
62 | |
63 | =head1 SEE ALSO |
64 | |
65 | L<Catalyst>. |
66 | |
67 | =head1 AUTHOR |
68 | |
69 | Marcus Ramberg, C<mramberg@cpan.org> |
70 | |
71 | =head1 THANK YOU |
72 | |
73 | SRI, for writing the awesome Catalyst framework |
74 | |
75 | =head1 COPYRIGHT |
76 | |
77 | This program is free software, you can redistribute it and/or modify it under |
78 | the same terms as Perl itself. |
79 | |
80 | =cut |
81 | |
82 | 1; |