The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Router::Dumb - yet another dumb path router for URLs

VERSION

version 0.006

SYNOPSIS

my $r = Router::Dumb->new;

$r->add_route(
  Router::Dumb::Route->new({
    parts       => [ qw(group :group uid :uid) ],
    target      => 'pants',
    constraints => {
      group => find_type_constraint('Int'),
    },
  }),
);

my $match = $r->route( '/group/123/uid/321' );

# $match->target  returns 'pants'
# $match->matches returns (group => 123, uid => 321)

DESCRIPTION

Router::Dumb provides a pretty dumb router. You can add routes and then ask how to route a given path string.

Routes have a path. A path is an arrayref of names. Names that start with a colon are placeholders. Everything else is a literal. Literals pieces must appear, literally, in the string being routed. A placeholder can be satisfied by any value, as long as it satisfies the placeholder's constraint. If there's no constraint, any value works.

The special part * can be used to mean "...then capture everything else into the placeholder named REST."

Most of the time, you won't be calling add_route, but using some other helper to figure out routes to add for you. Router::Dumb ships with Router::Dumb::Helper::FileMapper and Router::Dumb::Helper::RouteFile.

PERL VERSION

This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.

METHODS

add_route

$router->add_route(
  Router::Dumb::Route->new({
    parts  => [ qw( the :path parts ) ],
    target => 'target-string',
    constraints => {
      path => $moose_tc,
    },
  })
);

This method adds a new route to the router.

add_route_unless_exists

$router->add_route_unless_exists(
  Router::Dumb::Route->new({
    parts  => [ qw( the :path parts ) ],
    target => 'target-string',
    ...
  })
);

This method adds a new route to the router unless it would conflict, in which case it does nothing.

route

my $match_or_undef = $router->route( $str );

If the given string can be routed to a match, the match is returned. If not, the method returns false.

The string must begin with a /.

ordered_routes

my @routes = $router->ordered_routes;

This method returns the router's routes, in the order that they will be checked. You probably do not want to use this method unless you really know what you're doing.

AUTHOR

Ricardo Signes <cpan@semiotic.systems>

CONTRIBUTORS

  • Karen Etheridge <ether@cpan.org>

  • Ricardo Signes <rjbs@semiotic.systems>

COPYRIGHT AND LICENSE

This software is copyright (c) 2022 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.