1 package Catalyst::Plugin::SubRequest;
4 use Time::HiRes qw/tv_interval/;
10 Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst
14 use Catalyst 'SubRequest';
16 $c->subreq('/test/foo/bar', { template => 'magic.tt' });
18 $c->subreq( { path => '/test/foo/bar',
20 { template => 'magic.tt' });
24 Make subrequests to actions in Catalyst. Uses the catalyst
25 dispatcher, so it will work like an external url call.
31 =item subreq [path as string or hash ref], [stash as hash ref], [parameters as hash ref]
35 Takes a full path to a path you'd like to dispatch to.
36 If the path is passed as a hash ref then it can include body, action, match and path.
37 Any additional parameters are put into the stash.
43 *subreq = \&sub_request;
46 my ( $c, $path, $stash, $params ) = @_;
56 parameters => $params,
59 if (ref $path eq 'HASH') {
60 @request_mods{keys %$path} = values %$path;
62 $request_mods{path} = $path;
65 my $fake_engine = bless(
67 orig_request => $c->req,
68 request_mods => \%request_mods,
70 'Catalyst::Plugin::SubRequest::Internal::FakeEngine'
76 no warnings 'redefine';
78 local *{"${class}::engine"} = sub { $fake_engine };
80 my $inner_ctx = $class->prepare;
82 $inner_ctx->stash($stash || {});
86 begin => 'subrequest: /' . $path,
92 $c->stats->profile( end => 'subrequest: /' . $path ) if ($c->debug);
94 return $inner_ctx->response->body;
103 Marcus Ramberg, C<mramberg@cpan.org>
107 SRI, for writing the awesome Catalyst framework
111 This program is free software, you can redistribute it and/or modify it under
112 the same terms as Perl itself.
116 package # hide from PAUSE
117 Catalyst::Plugin::SubRequest::Internal::FakeEngine;
119 sub AUTOLOAD { return 1; } # yeah yeah yeah
123 my $req = $c->request;
125 @{$req}{keys %{$self->{orig_request}}} = values %{$self->{orig_request}};
126 while (my ($key,$value) = each %{$self->{request_mods}}) {
127 if (my $mut = $req->can($key)) {
130 $req->{$key} = $value;