Commit | Line | Data |
aae30f91 |
1 | package Catalyst::Plugin::SubRequest; |
2 | |
3 | use strict; |
61114b68 |
4 | use warnings; |
aae30f91 |
5 | |
d7361335 |
6 | our $VERSION = '0.17'; |
aae30f91 |
7 | |
aae30f91 |
8 | =head1 NAME |
9 | |
10 | Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst |
11 | |
12 | =head1 SYNOPSIS |
13 | |
14 | use Catalyst 'SubRequest'; |
15 | |
87c672db |
16 | my $res_body = $c->subreq('/test/foo/bar', { template => 'magic.tt' }); |
aae30f91 |
17 | |
87c672db |
18 | my $res_body = $c->subreq( { |
19 | path => '/test/foo/bar', |
20 | body => $body |
21 | }, { |
22 | template => 'magic.tt' |
23 | }); |
24 | |
25 | # Get the full response object |
26 | my $res = $c->subreq_res('/test/foo/bar', { |
27 | template => 'mailz.tt' |
28 | }, { |
29 | param1 => 23 |
30 | }); |
31 | $c->log->warn( $res->content_type ); |
4f38f6a7 |
32 | |
aae30f91 |
33 | =head1 DESCRIPTION |
34 | |
8c464987 |
35 | Make subrequests to actions in Catalyst. Uses the catalyst |
36 | dispatcher, so it will work like an external url call. |
87c672db |
37 | Methods are provided both to get the body of the response and the full |
38 | response (L<Catalyst::Response>) object. |
aae30f91 |
39 | |
40 | =head1 METHODS |
41 | |
a1e0150f |
42 | =over 4 |
aae30f91 |
43 | |
4f38f6a7 |
44 | =item subreq [path as string or hash ref], [stash as hash ref], [parameters as hash ref] |
aae30f91 |
45 | |
51589f61 |
46 | =item subrequest |
47 | |
aae30f91 |
48 | =item sub_request |
49 | |
4f38f6a7 |
50 | Takes a full path to a path you'd like to dispatch to. |
87c672db |
51 | |
52 | If the path is passed as a hash ref then it can include body, action, |
53 | match and path. |
54 | |
55 | An optional second argument as hashref can contain data to put into the |
56 | stash of the subrequest. |
57 | |
58 | An optional third argument as hashref can contain data to pass as |
59 | parameters to the subrequest. |
60 | |
61 | Returns the body of the response. |
62 | |
63 | =item subreq_res [path as string or hash ref], [stash as hash ref], [parameters as hash ref] |
64 | |
51589f61 |
65 | =item subrequest_response |
66 | |
87c672db |
67 | =item sub_request_response |
68 | |
69 | Like C<sub_request()>, but returns a full L<Catalyst::Response> object. |
aae30f91 |
70 | |
a1e0150f |
71 | =back |
aae30f91 |
72 | |
73 | =cut |
74 | |
75 | *subreq = \&sub_request; |
51589f61 |
76 | *subrequest = \&sub_request; |
87c672db |
77 | *subreq_res = \&sub_request_response; |
51589f61 |
78 | *subrequest_response = \&sub_request_response; |
aae30f91 |
79 | |
80 | sub sub_request { |
87c672db |
81 | return shift->sub_request_response( @_ )->body ; |
82 | } |
83 | |
84 | sub sub_request_response { |
885f6da0 |
85 | my ( $c, $path, $stash, $params ) = @_; |
7b9c5d16 |
86 | $stash ||= {}; |
87 | my $env = $c->request->env; |
88 | local $env->{PATH_INFO} = $path; |
89 | local $env->{REQUEST_URI} = $env->{SCRIPT_NAME} . $path; |
90 | $env->{REQUEST_URI} =~ s|//|/|g; |
91 | my $response_cb = $c->response->_response_cb; |
92 | my $class = ref($c) || $c; |
a1e0150f |
93 | |
6162c29a |
94 | $c->stats->profile( |
7b9c5d16 |
95 | begin => 'subrequest: ' . $path, |
6162c29a |
96 | comment => '', |
a1e0150f |
97 | ) if ($c->debug); |
98 | |
7b9c5d16 |
99 | my $i_ctx = $class->prepare(env => $env, response_cb => $response_cb); |
100 | $i_ctx->stash($stash); |
101 | $i_ctx->dispatch; |
102 | $i_ctx->finalize; |
6162c29a |
103 | |
7b9c5d16 |
104 | $c->stats->profile( end => 'subrequest: ' . $path ) if $c->debug; |
a1e0150f |
105 | |
7b9c5d16 |
106 | return $i_ctx->response; |
aae30f91 |
107 | } |
108 | |
109 | =head1 SEE ALSO |
110 | |
111 | L<Catalyst>. |
112 | |
61114b68 |
113 | =head1 AUTHORS |
aae30f91 |
114 | |
115 | Marcus Ramberg, C<mramberg@cpan.org> |
116 | |
61114b68 |
117 | Tomas Doran (t0m) C<< bobtfish@bobtfish.net >> |
118 | |
7b9c5d16 |
119 | =head1 MAINTAINERS |
120 | |
121 | Eden Cardim (edenc) C<eden@insoli.de> |
122 | |
aae30f91 |
123 | =head1 THANK YOU |
124 | |
125 | SRI, for writing the awesome Catalyst framework |
7b9c5d16 |
126 | MIYAGAWA, for writing the awesome Plack toolkit |
aae30f91 |
127 | |
128 | =head1 COPYRIGHT |
129 | |
d7361335 |
130 | Copyright (c) 2005 - 2011 |
61114b68 |
131 | the Catalyst::Plugin::SubRequest L</AUTHORS> |
85ec975f |
132 | as listed above. |
133 | |
134 | =head1 LICENSE |
135 | |
aae30f91 |
136 | This program is free software, you can redistribute it and/or modify it under |
137 | the same terms as Perl itself. |
138 | |
139 | =cut |
140 | |
141 | 1; |