controller method on application, this one is identical to that one in Context -...
Zbigniew Łukasiak [Wed, 18 Nov 2009 18:05:01 +0000 (18:05 +0000)]
lib/Catalyst.pm
lib/Catalyst/Context.pm
t/aggregate/unit_core_action_for.t

index 5af713a..6fe51c6 100644 (file)
@@ -262,6 +262,35 @@ e.g.
 
 =cut
 
+=head2 $c->controller($name)
+
+Gets a L<Catalyst::Controller> 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(@_);
index 3a1146c..a04b104 100644 (file)
@@ -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<Catalyst::Model> instance by name.
index c0af9d3..e3deb13 100644 (file)
@@ -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');
+