1 package Catalyst
::DispatchType
::Route
;
4 use base qw
/Catalyst::DispatchType/;
8 use Catalyst
::DispatchType
::RouteMapper
;
12 Catalyst::DispatchType::Route - RoR Route DispatchType
22 =head2 $self->list($c)
24 Debug output for Route dispatch points
29 my ( $self, $c ) = @_;
30 my $paths = Text
::SimpleTable
->new( [ 35, 'Route' ], [ 36, 'Private' ] );
31 foreach my $path ( sort keys %{ $self->{routes
} } ) {
32 my $display_path = $path eq '/' ?
$path : "/$path";
33 $paths->row( $display_path, "/" . $self->{routes
}->{$path} );
35 $c->log->debug( "Loaded Route actions:\n" . $paths->draw . "\n" )
36 if ( keys %{ $self->{routes
} } );
39 =head2 $self->match( $c, $path )
41 For each action registered to this exact path, offers the action a chance to
42 match the path (in the order in which they were registered). Succeeds on the
43 first action that matches, if any; if not, returns 0.
48 my ( $self, $c, $path ) = @_;
50 return 0 if @
{ $c->req->args };
55 #$c->log->debug('path=' . $path);
56 #$c->log->debug('ehre' . Dumper($self->{routes} ));
58 my $mapper = $self->{mapper
};
59 if (my $route = $mapper->parse($path)) {
60 #$c->log->debug('FOUDN!!! route=' . $route);
62 if (my $action = $self->{routes
}->{$route}) {;
63 #$c->log->debug('ACTION!');
64 $c->req->action($path);
65 $c->req->match($path);
67 $c->namespace( $action->namespace );
69 if (my $params = $mapper->getParams()) {
70 while (my($key, $val) = each %$params ) {
71 $c->req->param( $key => $val);
82 =head2 $self->register( $c, $action )
84 Calls register_path for every Path attribute for the given $action.
89 my ( $self, $c, $action ) = @_;
91 my @register = @
{ $action->attributes->{Route
} || [] };
93 $self->register_route( $c, $_, $action ) for @register;
95 return 1 if @register;
99 =head2 $self->register_path($c, $path, $action)
101 Registers an action at a given path.
106 my ( $self, $c, $route, $action ) = @_;
108 #$c->log->debug( 'path=' . $route . ' action=' . $action );
110 $self->{mapper
} ||= Catalyst
::DispatchType
::RouteMapper
->new();
112 my $mapper = $self->{ mapper
};
114 $self->{routes
} ||= {};
116 $route = $action->namespace . '/' . $route unless $route =~ s
/^\
///o
;
120 my @vars = split('/', $route);
122 foreach my $var ( @vars ) {
123 if ( $var =~ m/^\:(.*?)(?:\|(.*?)\|)?(\=(\?)?(.*))?$/o ) {
130 $map->{ constraints
}->{ $name } = $constraint if $constraint;
136 $map->{ defaults
}->{ $name } = $val if $isdefault && $val;
137 $map->{ params
}->{ $name } = $isdefault
143 push @new_vars, $var;
146 $route = join('/', @new_vars);
147 $self->{routes
}->{$route} = $action;
149 $map->{ path
} = $route;
150 $mapper->addMap($map);
154 #$c->log->debug(Dumper($map));
159 =head2 $self->uri_for_action($action, $captures)
161 get a URI part for an action; always returns undef is $captures is set
162 since Path actions don't have captures
167 my ( $self, $action, $captures ) = @_;
169 return undef if @
$captures;
171 if (my $paths = $action->attributes->{Path
}) {
172 my $path = $paths->[0];
173 $path = '/' unless length($path);
174 $path = "/${path}" unless ($path =~ m
/^\
//);
175 $path = URI
->new($path)->canonical;
188 This program is free software, you can redistribute it and/or modify it under
189 the same terms as Perl itself.