Changed default match to use path instead of result
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
index 1a1acbd..0fe6c32 100644 (file)
@@ -7,6 +7,7 @@ use Catalyst::Utils;
 use Catalyst::Action;
 use Catalyst::ActionContainer;
 use Catalyst::DispatchType::Default;
+use Catalyst::DispatchType::Index;
 use Text::ASCIITable;
 use Tree::Simple;
 use Tree::Simple::Visitor::FindByPath;
@@ -19,6 +20,9 @@ __PACKAGE__->mk_accessors(qw/tree dispatch_types/);
 # Preload these action types
 our @PRELOAD = qw/Path Regex/;
 
+# Postload these action types
+our @POSTLOAD = qw/Index Default/;
+
 =head1 NAME
 
 Catalyst::Dispatcher - The Catalyst Dispatcher
@@ -249,7 +253,7 @@ sub prepare_action {
 sub get_action {
     my ( $self, $c, $action, $namespace, $inherit ) = @_;
     return [] unless $action;
-    $namespace ||= '/';
+    $namespace ||= '';
     $inherit   ||= 0;
 
     my @match = $self->get_containers($namespace);
@@ -258,7 +262,19 @@ sub get_action {
 
     foreach my $child ( $inherit ? @match : $match[-1] ) {
         my $node = $child->actions;
-        push( @results, [ $node->{$action} ] ) if defined $node->{$action};
+        if ( defined $node->{$action} ) {
+            unless ($inherit) {
+                $namespace = '' if $namespace eq '/';
+                my $reverse = $node->{$action}->reverse;
+                my $name    = $namespace
+                  ? $namespace =~ /\/$/
+                  ? "$namespace$action"
+                  : "$namespace/$action"
+                  : $action;
+                last unless $name eq $reverse;
+            }
+            push( @results, [ $node->{$action} ] );
+        }
     }
     return \@results;
 }
@@ -455,8 +471,14 @@ sub setup_actions {
         }
     }
 
-    # Default actions are always last in the chain
-    push @{ $self->dispatch_types }, Catalyst::DispatchType::Default->new;
+    # Postload action types
+    for my $type (@POSTLOAD) {
+        my $class = "Catalyst::DispatchType::$type";
+        eval "require $class";
+        Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ )
+          if $@;
+        push @{ $self->dispatch_types }, $class->new;
+    }
 
     return unless $class->debug;