X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst.pm;fp=lib%2FCatalyst.pm;h=6fe51c6e283b699f47af395a57fe6c3d2dd52c8d;hb=75513d89c0f461d9ac3a678ef6c2321aefdc98cb;hp=5af713ae8308bde99f2d9ec21b4d0985dddab1e6;hpb=a07fc0d907345eabf7e3aa780c7e666f67c898e4;p=catagits%2FCatalyst-Runtime.git 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(@_);