1 package Catalyst::Plugin::SubRequest;
11 Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst
15 use Catalyst 'SubRequest';
17 my $res_body = $c->subreq('/test/foo/bar', { template => 'magic.tt' });
19 my $res_body = $c->subreq( {
20 path => '/test/foo/bar',
23 template => 'magic.tt'
26 # Get the full response object
27 my $res = $c->subreq_res('/test/foo/bar', {
28 template => 'mailz.tt'
32 $c->log->warn( $res->content_type );
36 Make subrequests to actions in Catalyst. Uses the catalyst
37 dispatcher, so it will work like an external url call.
38 Methods are provided both to get the body of the response and the full
39 response (L<Catalyst::Response>) object.
45 =item subreq [path as string or hash ref], [stash as hash ref], [parameters as hash ref]
51 Takes a full path to a path you'd like to dispatch to.
53 If the path is passed as a hash ref then it can include body, action,
56 An optional second argument as hashref can contain data to put into the
57 stash of the subrequest.
59 An optional third argument as hashref can contain data to pass as
60 parameters to the subrequest.
62 Returns the body of the response.
64 =item subreq_res [path as string or hash ref], [stash as hash ref], [parameters as hash ref]
66 =item subrequest_response
68 =item sub_request_response
70 Like C<sub_request()>, but returns a full L<Catalyst::Response> object.
76 *subreq = \&sub_request;
77 *subrequest = \&sub_request;
78 *subreq_res = \&sub_request_response;
79 *subrequest_response = \&sub_request_response;
82 return shift->sub_request_response(@_)->body;
85 sub sub_request_response {
86 my ( $c, $path, $stash, $params ) = @_;
88 my $env = $c->request->env;
89 my $req = Plack::Request->new($env);
91 $uri->query_form( $params || {} );
92 local $env->{QUERY_STRING} = $uri->query || '';
93 local $env->{PATH_INFO} = $path;
94 local $env->{REQUEST_URI} = $env->{SCRIPT_NAME} . $path;
96 # Jump through a few hoops for backcompat with pre 5.9007x
97 local($env->{&Catalyst::Middleware::Stash::PSGI_KEY}) = &Catalyst::Middleware::Stash::_create_stash()
98 if $INC{'Catalyst/Middleware/Stash.pm'};
100 $env->{REQUEST_URI} =~ s|//|/|g;
101 my $class = ref($c) || $c;
104 begin => 'subrequest: ' . $path,
109 my $writer = Catalyst::Plugin::SubRequest::Writer->new;
110 my $response_cb = sub {
111 my $response = shift;
112 my ($status, $headers, $body) = @$response;
120 my $i_ctx = $class->prepare( env => $env, response_cb => $response_cb );
121 $i_ctx->stash($stash);
124 $c->stats->profile( end => 'subrequest: ' . $path ) if $c->debug;
126 if($writer->_is_closed) {
127 $i_ctx->response->body($writer->body);
130 return $i_ctx->response;
134 package Catalyst::Plugin::SubRequest::Writer;
139 traits => ['String'],
141 handles => { write => 'append' }
143 has _is_closed => ( isa => 'Bool', is => 'rw', default => 0 );
144 sub close { shift->_is_closed(1) }
146 around write => sub {
149 return if $self->_is_closed;
159 Marcus Ramberg, C<mramberg@cpan.org>
161 Tomas Doran (t0m) C<< bobtfish@bobtfish.net >>
165 Eden Cardim (edenc) C<eden@insoli.de>
169 SRI, for writing the awesome Catalyst framework
171 MIYAGAWA, for writing the awesome Plack toolkit
175 Copyright (c) 2005 - 2011
176 the Catalyst::Plugin::SubRequest L</AUTHORS>
181 This program is free software, you can redistribute it and/or modify it under
182 the same terms as Perl itself.