1 package Catalyst::Plugin::SubRequest;
10 Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst
14 use Catalyst 'SubRequest';
16 my $res_body = $c->subreq('/test/foo/bar', { template => 'magic.tt' });
18 my $res_body = $c->subreq( {
19 path => '/test/foo/bar',
22 template => 'magic.tt'
25 # Get the full response object
26 my $res = $c->subreq_res('/test/foo/bar', {
27 template => 'mailz.tt'
31 $c->log->warn( $res->content_type );
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.
44 =item subreq [path as string or hash ref], [stash as hash ref], [parameters as hash ref]
50 Takes a full path to a path you'd like to dispatch to.
52 If the path is passed as a hash ref then it can include body, action,
55 An optional second argument as hashref can contain data to put into the
56 stash of the subrequest.
58 An optional third argument as hashref can contain data to pass as
59 parameters to the subrequest.
61 Returns the body of the response.
63 =item subreq_res [path as string or hash ref], [stash as hash ref], [parameters as hash ref]
65 =item subrequest_response
67 =item sub_request_response
69 Like C<sub_request()>, but returns a full L<Catalyst::Response> object.
75 *subreq = \&sub_request;
76 *subrequest = \&sub_request;
77 *subreq_res = \&sub_request_response;
78 *subrequest_response = \&sub_request_response;
81 return shift->sub_request_response( @_ )->body ;
84 sub sub_request_response {
85 my ( $c, $path, $stash, $params ) = @_;
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;
95 begin => 'subrequest: ' . $path,
99 my $i_ctx = $class->prepare(env => $env, response_cb => $response_cb);
100 $i_ctx->stash($stash);
104 $c->stats->profile( end => 'subrequest: ' . $path ) if $c->debug;
106 return $i_ctx->response;
115 Marcus Ramberg, C<mramberg@cpan.org>
117 Tomas Doran (t0m) C<< bobtfish@bobtfish.net >>
121 Eden Cardim (edenc) C<eden@insoli.de>
125 SRI, for writing the awesome Catalyst framework
126 MIYAGAWA, for writing the awesome Plack toolkit
130 Copyright (c) 2005 - 2011
131 the Catalyst::Plugin::SubRequest L</AUTHORS>
136 This program is free software, you can redistribute it and/or modify it under
137 the same terms as Perl itself.