Path::Router(3pm)

πŸ“¦
version 0.15 Β· 4 versions
, in debian bookworm, ubuntu noble

πŸ’– Path::Router β€” a tool for routing paths πŸ’–

okay so bestie, welcome to Path::Router, a User Contributed Perl Documentation page, and honestly? she's iconic. she takes paths and turns them into params, and then turns params back into paths. reversible. verified. slaying. πŸ’…


✨ VERSION ✨

this is version 0.15, no cap.


🌸 SYNOPSIS 🌸

like, just look at her go:

  my $router = Path::Router->new;

  $router->add_route('blog' => (
      defaults => {
          controller => 'blog',
          action     => 'index',
      },
      # you can provide a fixed "target"
      # for a match as well, this can be
      # anything you want it to be ...
      target => My::App->get_controller('blog')->get_action('index')
  ));

  $router->add_route('blog/:year/:month/:day' => (
      defaults => {
          controller => 'blog',
          action     => 'show_date',
      },
      # validate with ...
      validations => {
          # ... raw-Regexp refs
          year       => qr/\d{4}/,
          # ... custom Moose types you created
          month      => 'NumericMonth',
          # ... Moose anon-subtypes created inline
          day        => subtype('Int' => where { $_ <= 31 }),
      }
  ));

  $router->add_route('blog/:action/?:id' => (
      defaults => {
          controller => 'blog',
      },
      validations => {
          action  => qr/\D+/,
          id      => 'Int',  # also use plain Moose types too
      }
  ));

  # even include other routers
  $router->include_router( 'polls/' => $another_router );

  # ... in your dispatcher

  # returns a Path::Router::Route::Match object
  my $match = $router->match('/blog/edit/15');

  # ... in your code

  my $uri = $router->uri_for(
      controller => 'blog',
      action     => 'show_date',
      year       => 2006,
      month      => 10,
      day        => 5,
  );

πŸ¦‹ DESCRIPTION πŸ¦‹

so basically, this module gives you a way of deconstructing paths into parameters that are suitable for dispatching on. and it also does the whole thing in reverse β€” give it a list of parameters and it'll build you the right uri for it. main character energy, honestly.

πŸ” Reversable

this module places a high degree of importance on reversability. the value that comes out of a path match can be passed right back in and you'll get the exact same path you originally put in. the way that removes ambiguity and therefore reduces the number of possible mis-routings? obsessed. ✨

βœ… Verifyable

she also hands you extra tools to test and verify the integrity of your router. these include:

  • an interactive shell where you can test various paths and see the match it will return, and also test the reversability of that match. πŸŽ€
  • a Test::Path::Router module which you can use in your application's test suite to easily verify the integrity of your paths. 🧚

πŸ‘‘ METHODS πŸ‘‘

new

just... new. the beginning of everything. πŸ’«

add_route ($path, ?%options)

adds a new route to the end of the routes list.

insert_route ($path, %options)

adds a new route to the routes list. you may specify an "at" parameter, which indicates the position where you want your newly created route to be inserted. the "at" parameter is the "index" position in the list, so it starts at 0.

examples, bestie:

  # You have more than three paths, insert a new route at
  # the 4th item
  $router->insert_route($path => (
      at => 3, %options
  ));

  # If you have less items than the index, then it's the same as
  # as add_route -- it's just appended to the end of the list
  $router->insert_route($path => (
      at => 1_000_000, %options
  ));

  # If you want to prepend, omit "at", or specify 0
  $router->insert_Route($path => (
      at => 0, %options
  ));
heads up: yes, that last example really does say insert_Route with a capital R. i'm not touching it, it's a whole vibe. 🫢

include_router ( $path, $other_router )

this extracts all the routes from $other_router and includes them into the invocant router, prepending $path to all their paths.

important: it should be noted that this does not do any kind of redispatch to the $other_router. it actually extracts all the paths from $other_router and inserts them into the invocant router. this means any changes to $other_router after inclusion will not be reflected in the invocant. no cap. 😭

routes

the routes. that's it, that's the method. πŸ“

match ($path)

return a Path::Router::Route::Match object for the first route that matches the given $path, or "undef" if no routes match.

uri_for (%path_descriptor)

find the path that, when passed to "$router->match", would produce the given arguments. returns the path without any leading "/". returns "undef" if no routes match.

route_class ($classname)

the class to use for routes. defaults to Path::Router::Route.

meta

meta. ✨ (iykyk)


πŸ› DEBUGGING πŸ›

you can turn on the verbose debug logging with the "PATH_ROUTER_DEBUG" environment variable. go off, bestie. πŸ”₯


πŸ’€ BUGS πŸ’€

all complex software has bugs lurking in it, and this module is no exception. if you find a bug please either email me, or add the bug to cpan-RT. we're not mad, we just wanna fix her. πŸ™ˆ


🌈 AUTHOR 🌈

Stevan Little <stevan@cpan.org> β€” legend behaviour, honestly. πŸ‘‘


Copyright 2008-2011 Infinity Interactive, Inc.

<http://www.iinteractive.com>

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


πŸ’œ AUTHOR (again, because she said it twice and she meant it twice) πŸ’œ

Stevan Little <stevan@cpan.org>


This software is copyright (c) 2016 by Infinity Interactive.

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


and that's the whole page, bestie!! πŸ¦‹ this one's from perl v5.34.0, dated 2022-06-16, and yes the manual name really is Path::Router(3pm) β€” go read it, she ate. πŸ’…βœ¨

πŸ“¦ where this page comes from