1 package Catalyst::DispatchType::Path;
4 extends 'Catalyst::DispatchType';
14 default => sub { +{} },
21 Catalyst::DispatchType::Path - Path DispatchType
25 See L<Catalyst::DispatchType>.
29 Dispatch type managing full path matching behaviour. For more information on
34 =item * L<Catalyst::Manual::Intro> for how they affect application authors
36 =item * L<Catalyst::DispatchType> for implementation information.
42 =head2 $self->list($c)
44 Debug output for Path dispatch points
49 my ( $self, $c ) = @_;
50 my $avail_width = Catalyst::Utils::term_width() - 9;
51 my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50);
52 my $col2_width = $avail_width - $col1_width;
53 my $paths = Text::SimpleTable->new(
54 [ $col1_width, 'Path' ], [ $col2_width, 'Private' ]
56 foreach my $path ( sort keys %{ $self->_paths } ) {
57 foreach my $action ( @{ $self->_paths->{$path} } ) {
58 my $args = $action->attributes->{Args}->[0];
59 my $parts = defined($args) ? '/*' x $args : '/...';
61 my $display_path = "/$path/$parts";
62 $display_path =~ s{/{1,}}{/}g;
64 $paths->row( $display_path, "/$action" );
67 $c->log->debug( "Loaded Path actions:\n" . $paths->draw . "\n" )
68 if ( keys %{ $self->_paths } );
71 =head2 $self->match( $c, $path )
73 For each action registered to this exact path, offers the action a chance to
74 match the path (in the order in which they were registered). Succeeds on the
75 first action that matches, if any; if not, returns 0.
80 my ( $self, $c, $path ) = @_;
82 $path = '/' if !defined $path || !length $path;
84 my @actions = @{ $self->_paths->{$path} || [] };
86 foreach my $action ( @actions ) {
87 next unless $action->match($c);
88 $c->req->action($path);
89 $c->req->match($path);
91 $c->namespace( $action->namespace );
98 =head2 $self->register( $c, $action )
100 Calls register_path for every Path attribute for the given $action.
105 my ( $self, $c, $action ) = @_;
107 my @register = @{ $action->attributes->{Path} || [] };
109 $self->register_path( $c, $_, $action ) for @register;
111 return 1 if @register;
115 =head2 $self->register_path($c, $path, $action)
117 Registers an action at a given path.
122 my ( $self, $c, $path, $action ) = @_;
124 $path = '/' unless length $path;
125 $path = URI->new($path)->canonical;
126 $path =~ s{(?<=[^/])/+\z}{};
128 $self->_paths->{$path} = [
129 sort { $a->compare($b) } ($action, @{ $self->_paths->{$path} || [] })
135 =head2 $self->uri_for_action($action, $captures)
137 get a URI part for an action; always returns undef is $captures is set
138 since Path actions don't have captures
143 my ( $self, $action, $captures ) = @_;
145 return undef if @$captures;
147 if (my $paths = $action->attributes->{Path}) {
148 my $path = $paths->[0];
149 $path = '/' unless length($path);
150 $path = "/${path}" unless ($path =~ m/^\//);
151 $path = URI->new($path)->canonical;
160 Catalyst Contributors, see Catalyst.pm
164 This library is free software. You can redistribute it and/or modify it under
165 the same terms as Perl itself.
169 __PACKAGE__->meta->make_immutable;