docs and allow ../
John Napiorkowski [Thu, 15 Dec 2016 15:24:57 +0000 (09:24 -0600)]
lib/Catalyst/Controller.pm
lib/Catalyst/Dispatcher.pm
t/relative_root_action_for_bug.t

index ad88a51..fbab60f 100644 (file)
@@ -662,10 +662,25 @@ arguments, when it is instantiated:
 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)
 
index 3ed3267..d3f25a3 100644 (file)
@@ -407,9 +407,14 @@ sub prepare_action {
       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
 
@@ -426,10 +431,15 @@ sub get_action {
 
 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};
index 9443052..8db0f1f 100644 (file)
@@ -17,11 +17,6 @@ use Test::Most;
       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=>'');
@@ -62,9 +57,10 @@ use Test::Most;
     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;
@@ -77,9 +73,9 @@ use Test::Most;
 
 use Catalyst::Test 'MyApp';
 
+ok request '/top';
 ok request '/story';
 ok request '/author';
-ok request '/top';
 
-done_testing(8);
+done_testing(10);