1 package Catalyst::Plugin::SubRequest;
5 use Time::HiRes qw/tv_interval/;
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 ) = @_;
96 parameters => $params,
99 if (ref $path eq 'HASH') {
100 @request_mods{keys %$path} = values %$path;
101 $path = $path->{path};
103 $request_mods{path} = $path;
105 $request_mods{_body} = delete $request_mods{body};
107 my $fake_engine = bless(
109 orig_request => $c->req,
110 request_mods => \%request_mods,
112 'Catalyst::Plugin::SubRequest::Internal::FakeEngine'
118 no warnings 'redefine';
120 local *{"${class}::engine"} = sub { $fake_engine };
122 my $inner_ctx = $class->prepare;
124 $inner_ctx->stash($stash || {});
128 begin => 'subrequest: /' . $path,
132 $inner_ctx->dispatch;
134 $c->stats->profile( end => 'subrequest: /' . $path ) if ($c->debug);
136 return $inner_ctx->response;
145 Marcus Ramberg, C<mramberg@cpan.org>
147 Tomas Doran (t0m) C<< bobtfish@bobtfish.net >>
151 SRI, for writing the awesome Catalyst framework
155 Copyright (c) 2005 - 2011
156 the Catalyst::Plugin::SubRequest L</AUTHORS>
161 This program is free software, you can redistribute it and/or modify it under
162 the same terms as Perl itself.
166 package # hide from PAUSE
167 Catalyst::Plugin::SubRequest::Internal::FakeEngine;
169 sub AUTOLOAD { return 1; } # yeah yeah yeah
173 my $req = $c->request;
175 @{$req}{keys %{$self->{orig_request}}} = values %{$self->{orig_request}};
176 while (my ($key,$value) = each %{$self->{request_mods}}) {
177 if (my $mut = $req->can($key)) {
180 $req->{$key} = $value;