5725374f873b905358b8e651c8650e3681bff0a3
[catagits/Catalyst-Plugin-SubRequest.git] / lib / Catalyst / Plugin / SubRequest.pm
1 package Catalyst::Plugin::SubRequest;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.17';
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     my $res_body = $c->subreq('/test/foo/bar', { template => 'magic.tt' });
17
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 );
32
33 =head1 DESCRIPTION
34
35 Make subrequests to actions in Catalyst. Uses the  catalyst
36 dispatcher, so it will work like an external url call.
37 Methods are provided both to get the body of the response and the full
38 response (L<Catalyst::Response>) object.
39
40 =head1 METHODS
41
42 =over 4
43
44 =item subreq [path as string or hash ref], [stash as hash ref], [parameters as hash ref]
45
46 =item subrequest
47
48 =item sub_request
49
50 Takes a full path to a path you'd like to dispatch to.
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
65 =item subrequest_response
66
67 =item sub_request_response
68
69 Like C<sub_request()>, but returns a full L<Catalyst::Response> object.
70
71 =back
72
73 =cut
74
75 *subreq = \&sub_request;
76 *subrequest = \&sub_request;
77 *subreq_res = \&sub_request_response;
78 *subrequest_response = \&sub_request_response;
79
80 sub sub_request {
81     return shift->sub_request_response( @_ )->body ;
82 }
83
84 sub sub_request_response {
85     my ( $c, $path, $stash, $params ) = @_;
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;
93
94     $c->stats->profile(
95         begin   => 'subrequest: ' . $path,
96         comment => '',
97     ) if ($c->debug);
98
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;
103
104     $c->stats->profile( end => 'subrequest: ' . $path ) if $c->debug;
105
106     return $i_ctx->response;
107 }
108
109 =head1 SEE ALSO
110
111 L<Catalyst>.
112
113 =head1 AUTHORS
114
115 Marcus Ramberg, C<mramberg@cpan.org>
116
117 Tomas Doran (t0m) C<< bobtfish@bobtfish.net >>
118
119 =head1 MAINTAINERS
120
121 Eden Cardim (edenc) C<eden@insoli.de>
122
123 =head1 THANK YOU
124
125 SRI, for writing the awesome Catalyst framework
126 MIYAGAWA, for writing the awesome Plack toolkit
127
128 =head1 COPYRIGHT
129
130 Copyright (c) 2005 - 2011
131 the Catalyst::Plugin::SubRequest L</AUTHORS>
132 as listed above.
133
134 =head1 LICENSE
135
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;