From L<Catalyst::Component::ApplicationAttribute>, stashes the application
instance as $self->_application.
-=head2 $self->action_for('name')
+=head2 $self->action_for($action_name)
-Returns the Catalyst::Action object (if any) for a given method name
-in this component.
+Returns the Catalyst::Action object (if any) for a given action in this
+controller or relative to it. You may refer to actions in controllers
+nested under the current controllers namespace, or in controllers 'up'
+from the current controller namespace. For example:
+
+ package MyApp::Controller::One::Two;
+ use base 'Catalyst::Controller';
+
+ sub foo :Local {
+ my ($self, $c) = @_;
+ $self->action_for('foo'); # action 'foo' in Controller 'One::Two'
+ $self->action_for('three/bar'); # action 'bar' in Controller 'One::Two::Three'
+ $self->action_for('../boo'); # action 'boo' in Controller 'One'
+ }
+
+This returns 'undef' if there is no action matching the requested action
+name (after any path normalization) so you should check for this as needed.
=head2 $self->action_namespace($c)
if ( $c->debug && @args );
}
-=head2 $self->get_action( $action, $namespace )
+=head2 $self->get_action( $action_name, $namespace )
-returns a named action from a given namespace.
+returns a named action from a given namespace. C<$action_name>
+may be a relative path on that C<$namespace> such as
+
+ $self->get_action('../bar', 'foo/baz');
+
+In which case we look for the action at 'foo/bar'.
=cut
Returns the named action by its full private path.
+This method performs some normalization on C<$path> so that if
+it includes '..' it will do the right thing (for example if
+C<$path> is '/foo/../bar' that is normalized to '/bar'.
+
=cut
sub get_action_by_path {
my ( $self, $path ) = @_;
+ $path =~s/[^\/]+\/\.\.\/// while $path=~m/\.\./;
$path =~ s/^\///;
$path = "/$path" unless $path =~ /\//;
$self->_action_hash->{$path};
my ($self, $c) = @_;
Test::Most::is $self->action_for('top'), 'top';
Test::Most::is $self->action_for('story/story'), 'story/story';
-
- #warn ref($c)->dispatcher->get_action('story/story', '/root');
-
- #use Devel::Dwarn;
- #Dwarn ref($c)->dispatcher->_action_hash->{'story/story'};
}
MyApp::Controller::Root->config(namespace=>'');
sub author :Chained(root) Args(0) {
my ($self, $c, $id) = @_;
Test::Most::is $self->action_for('author'), 'story/author/author';
+ Test::Most::is $self->action_for('../story'), 'story/story';
+ Test::Most::is $self->action_for('../../top'), 'top';
}
-
__PACKAGE__->meta->make_immutable;
package MyApp;
use Catalyst::Test 'MyApp';
+ok request '/top';
ok request '/story';
ok request '/author';
-ok request '/top';
-done_testing(8);
+done_testing(10);