my $class = (exists $args{attributes}{ActionClass}
? $args{attributes}{ActionClass}[0]
: $self->_action_class);
-
Class::MOP::load_class($class);
- return $class->new( \%args );
+
+ my $action_args = $self->config->{action_args};
+ my %extra_args = (
+ %{ $action_args->{'*'} || {} },
+ %{ $action_args->{ $args{name} } || {} },
+ );
+
+ return $class->new({ %extra_args, %args });
}
sub _parse_attrs {
Sets 'path_prefix', as described below.
+=head2 action_args
+
+Allows you to set constructor arguments on your actions. You can set arguments
+globally (for all actions of the controller) and specifically (for a single
+action). This is particularly useful when using C<ActionRole>s
+(L<Catalyst::Controller::ActionRole>) and custom C<ActionClass>es.
+
+ __PACKAGE__->config(
+ action_args => {
+ '*' => { globalarg1 => 'hello', globalarg2 => 'goodbye' },
+ 'specific_action' => { customarg => 'arg1' },
+ },
+ );
+
+In the case above the action class associated with C<specific_action> would get
+passed the following arguments, in addition to the normal action constructor
+arguments, when it is instantiated:
+
+ (globalarg1 => 'hello', globalarg2 => 'goodbye', customarg => 'arg1')
+
=head1 METHODS
=head2 BUILDARGS ($app, @args)
BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
-use Test::More tests => 42 * $iters;
+use Test::More;
use Catalyst::Test 'TestApp';
if ( $ENV{CAT_BENCHMARK} ) {
);
}
+ {
+ ok( my $response = request('http://localhost/action_action_seven'),
+ 'Request' );
+ ok( $response->is_success, 'Response Successful 2xx' );
+ is( $response->content_type, 'text/plain', 'Response Content-Type' );
+ is( $response->header('X-Catalyst-Action'),
+ 'action_action_seven', 'Test Action' );
+ is(
+ $response->header('X-Test-Class'),
+ 'TestApp::Controller::Action::Action',
+ 'Test Class'
+ );
+ is( $response->header('X-TestExtraArgsAction'), '42,23', 'Extra args get passed to action contstructor' );
+ like(
+ $response->content,
+ qr/^bless\( .* 'Catalyst::Request' \)$/s,
+ 'Content is a serialized Catalyst::Request'
+ );
+ }
}
+
+done_testing;
--- /dev/null
+package TestApp::Action::TestExtraArgsAction;
+
+use Moose;
+use namespace::autoclean;
+
+extends 'Catalyst::Action';
+
+has [qw/extra_arg another_extra_arg/] => (is => 'ro');
+
+after execute => sub {
+ my ($self, $controller, $ctx) = @_;
+ $ctx->response->header('X-TestExtraArgsAction' => join q{,} => $self->extra_arg, $self->another_extra_arg);
+};
+
+__PACKAGE__->meta->make_immutable;
+
+1;
use strict;
use base 'TestApp::Controller::Action';
-__PACKAGE__->config( actions => { action_action_five => { ActionClass => '+Catalyst::Action::TestBefore' } } );
+__PACKAGE__->config(
+ actions => {
+ action_action_five => { ActionClass => '+Catalyst::Action::TestBefore' },
+ },
+ action_args => {
+ '*' => { extra_arg => 42 },
+ action_action_seven => { another_extra_arg => 23 },
+ },
+);
sub action_action_one : Global : ActionClass('TestBefore') {
my ( $self, $c ) = @_;
$c->forward('TestApp::View::Dump::Request');
}
+sub action_action_seven : Global : ActionClass('~TestExtraArgsAction') {
+ my ( $self, $c ) = @_;
+ $c->forward('TestApp::View::Dump::Request');
+}
+
1;