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 $c->subreq('/test/foo/bar', { template => 'magic.tt' });
19 $c->subreq( { path => '/test/foo/bar',
21 { template => 'magic.tt' });
25 Make subrequests to actions in Catalyst. Uses the catalyst
26 dispatcher, so it will work like an external url call.
32 =item subreq [path as string or hash ref], [stash as hash ref], [parameters as hash ref]
36 Takes a full path to a path you'd like to dispatch to.
37 If the path is passed as a hash ref then it can include body, action, match and path.
38 Any additional parameters are put into the stash.
44 *subreq = \&sub_request;
47 my ( $c, $path, $stash, $params ) = @_;
57 parameters => $params,
60 if (ref $path eq 'HASH') {
61 @request_mods{keys %$path} = values %$path;
63 $request_mods{path} = $path;
65 $request_mods{_body} = delete $request_mods{body};
67 my $fake_engine = bless(
69 orig_request => $c->req,
70 request_mods => \%request_mods,
72 'Catalyst::Plugin::SubRequest::Internal::FakeEngine'
78 no warnings 'redefine';
80 local *{"${class}::engine"} = sub { $fake_engine };
82 my $inner_ctx = $class->prepare;
84 $inner_ctx->stash($stash || {});
88 begin => 'subrequest: /' . $path,
94 $c->stats->profile( end => 'subrequest: /' . $path ) if ($c->debug);
96 return $inner_ctx->response->body;
105 Marcus Ramberg, C<mramberg@cpan.org>
107 Tomas Doran (t0m) C<< bobtfish@bobtfish.net >>
111 SRI, for writing the awesome Catalyst framework
115 Copyright (c) 2005 - 2008
116 the Catalyst::Plugin::SubRequest L</AUTHORS>
121 This program is free software, you can redistribute it and/or modify it under
122 the same terms as Perl itself.
126 package # hide from PAUSE
127 Catalyst::Plugin::SubRequest::Internal::FakeEngine;
129 sub AUTOLOAD { return 1; } # yeah yeah yeah
133 my $req = $c->request;
135 @{$req}{keys %{$self->{orig_request}}} = values %{$self->{orig_request}};
136 while (my ($key,$value) = each %{$self->{request_mods}}) {
137 if (my $mut = $req->can($key)) {
140 $req->{$key} = $value;