From: Zbigniew Łukasiak Date: Wed, 18 Nov 2009 18:05:01 +0000 (+0000) Subject: controller method on application, this one is identical to that one in Context -... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=75513d89c0f461d9ac3a678ef6c2321aefdc98cb controller method on application, this one is identical to that one in Context - so it can be proxied there --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 5af713a..6fe51c6 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -262,6 +262,35 @@ e.g. =cut +=head2 $c->controller($name) + +Gets a L instance by name. + + $c->controller('Foo')->do_stuff; + +If the name is omitted, will return the controller for the dispatched +action. + +If you want to search for controllers, pass in a regexp as the argument. + + # find all controllers that start with Foo + my @foo_controllers = $c->controller(qr{^Foo}); + + +=cut + +sub controller { + my ( $c, $name, @args ) = @_; + + if( $name ) { + my @result = $c->_comp_search_prefixes( $name, qw/Controller C/ ); + return map { $c->_filter_component( $_, @args ) } @result if ref $name; + return $c->_filter_component( $result[ 0 ], @args ); + } + + return $c->component( $c->action->class ); +} + sub _comp_search_prefixes { my $c = shift; return map $c->components->{ $_ }, $c->_comp_names_search_prefixes(@_); diff --git a/lib/Catalyst/Context.pm b/lib/Catalyst/Context.pm index 3a1146c..a04b104 100644 --- a/lib/Catalyst/Context.pm +++ b/lib/Catalyst/Context.pm @@ -35,6 +35,7 @@ has 'application' => ( handles => [ qw/ controllers + controller models views component @@ -345,18 +346,6 @@ If you want to search for controllers, pass in a regexp as the argument. =cut -sub controller { - my ( $c, $name, @args ) = @_; - - if( $name ) { - my @result = $c->_comp_search_prefixes( $name, qw/Controller C/ ); - return map { $c->_filter_component( $_, @args ) } @result if ref $name; - return $c->_filter_component( $result[ 0 ], @args ); - } - - return $c->component( $c->action->class ); -} - =head2 $c->model($name) Gets a L instance by name. diff --git a/t/aggregate/unit_core_action_for.t b/t/aggregate/unit_core_action_for.t index c0af9d3..e3deb13 100644 --- a/t/aggregate/unit_core_action_for.t +++ b/t/aggregate/unit_core_action_for.t @@ -8,7 +8,7 @@ use lib "$FindBin::Bin/../lib"; use Test::More; -plan tests => 4; +plan tests => 6; use_ok('TestApp'); @@ -21,3 +21,12 @@ is(TestApp->controller('Args')->action_for('args')->code, is(TestApp->controller('Args')->action_for('args').'', 'args/args', 'action stringifies'); + +my $controller = Catalyst::Context->new( application => TestApp->new )->controller('Args'); +is($controller->action_for('args')->code, + TestApp::Controller::Args->can('args'), + 'action_for on controller ok'); +is($controller->action_for('args').'', + 'args/args', + 'action stringifies'); +