major refactor, all tests passing
[catagits/Catalyst-Plugin-SubRequest.git] / lib / Catalyst / Plugin / SubRequest.pm
CommitLineData
aae30f91 1package Catalyst::Plugin::SubRequest;
2
3use strict;
61114b68 4use warnings;
aae30f91 5
d7361335 6our $VERSION = '0.17';
aae30f91 7
aae30f91 8=head1 NAME
9
10Catalyst::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 35Make subrequests to actions in Catalyst. Uses the catalyst
36dispatcher, so it will work like an external url call.
87c672db 37Methods are provided both to get the body of the response and the full
38response (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 50Takes a full path to a path you'd like to dispatch to.
87c672db 51
52If the path is passed as a hash ref then it can include body, action,
53match and path.
54
55An optional second argument as hashref can contain data to put into the
56stash of the subrequest.
57
58An optional third argument as hashref can contain data to pass as
59parameters to the subrequest.
60
61Returns 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
69Like 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
80sub sub_request {
87c672db 81 return shift->sub_request_response( @_ )->body ;
82}
83
84sub 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
111L<Catalyst>.
112
61114b68 113=head1 AUTHORS
aae30f91 114
115Marcus Ramberg, C<mramberg@cpan.org>
116
61114b68 117Tomas Doran (t0m) C<< bobtfish@bobtfish.net >>
118
7b9c5d16 119=head1 MAINTAINERS
120
121Eden Cardim (edenc) C<eden@insoli.de>
122
aae30f91 123=head1 THANK YOU
124
125SRI, for writing the awesome Catalyst framework
7b9c5d16 126MIYAGAWA, for writing the awesome Plack toolkit
aae30f91 127
128=head1 COPYRIGHT
129
d7361335 130Copyright (c) 2005 - 2011
61114b68 131the Catalyst::Plugin::SubRequest L</AUTHORS>
85ec975f 132as listed above.
133
134=head1 LICENSE
135
aae30f91 136This program is free software, you can redistribute it and/or modify it under
137the same terms as Perl itself.
138
139=cut
140
1411;