1 package Catalyst::Plugin::SubRequest;
9 Catalyst::Plugin::SubRequest - Make subrequests to actions in Catalyst
13 use Catalyst 'SubRequest';
15 $c->subreq('/test/foo/bar', { template => 'magic.tt' });
19 Make subrequests to actions in Catalyst. Uses the catalyst
20 dispatcher, so it will work like an external url call.
26 =item subreq path, [stash as hash ref], [parameters as hash ref]
30 Takes a full path to a path you'd like to dispatch to. Any additional
31 parameters are put into the stash.
37 *subreq = \&sub_request;
40 my ( $c, $path, $stash, $params ) = @_;
50 parameters => $params,
53 if (ref $path eq 'HASH') {
54 @request_mods{keys %$path} = values %$path;
56 $request_mods{path} = $path;
59 my $fake_engine = bless(
61 orig_request => $c->req,
62 request_mods => \%request_mods,
64 'Catalyst::Plugin::SubRequest::Internal::FakeEngine'
70 no warnings 'redefine';
72 local *{"${class}::engine"} = sub { $fake_engine };
74 my $inner_ctx = $class->prepare;
76 $inner_ctx->stash($stash || {});
79 return $inner_ctx->response->body;
88 Marcus Ramberg, C<mramberg@cpan.org>
92 SRI, for writing the awesome Catalyst framework
96 This program is free software, you can redistribute it and/or modify it under
97 the same terms as Perl itself.
101 package # hide from PAUSE
102 Catalyst::Plugin::SubRequest::Internal::FakeEngine;
104 sub AUTOLOAD { return 1; } # yeah yeah yeah
108 my $req = $c->request;
109 my %attrs = (%{$self->{orig_request}}, %{$self->{request_mods}});
110 @{$req}{keys %attrs} = values %attrs;