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