{"id":18779410,"url":"https://github.com/zostay/raku-path-router","last_synced_at":"2025-10-25T21:04:18.789Z","repository":{"id":32543452,"uuid":"36125548","full_name":"zostay/raku-Path-Router","owner":"zostay","description":"A tool for routing paths","archived":false,"fork":false,"pushed_at":"2020-03-26T13:38:33.000Z","size":101,"stargazers_count":2,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T11:40:52.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Raku","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"artistic-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zostay.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-23T13:57:20.000Z","updated_at":"2021-05-14T15:57:34.000Z","dependencies_parsed_at":"2022-09-12T05:01:21.223Z","dependency_job_id":null,"html_url":"https://github.com/zostay/raku-Path-Router","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/zostay/raku-Path-Router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zostay%2Fraku-Path-Router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zostay%2Fraku-Path-Router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zostay%2Fraku-Path-Router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zostay%2Fraku-Path-Router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zostay","download_url":"https://codeload.github.com/zostay/raku-Path-Router/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zostay%2Fraku-Path-Router/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262699040,"owners_count":23350232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T20:19:59.584Z","updated_at":"2025-10-25T21:04:18.718Z","avatar_url":"https://github.com/zostay.png","language":"Raku","funding_links":[],"categories":[],"sub_categories":[],"readme":"NAME\n====\n\nPath::Router - A tool for routing paths\n\nSYNOPSIS\n========\n\n    my $router = Path::Router.new;\n\n    $router.add-route('blog' =\u003e %(\n        conditions =\u003e %( :method\u003cGET\u003e ),\n        defaults =\u003e {\n            controller =\u003e 'blog',\n            action     =\u003e 'index',\n        },\n        # you can provide a fixed \"target\"\n        # for a match as well, this can be\n        # anything you want it to be ...\n        target =\u003e My::App.get_controller('blog').get_action('index')\n    ));\n\n    $router.add-route('blog/:year/:month/:day' =\u003e %(\n        conditions =\u003e %( :method\u003cGET\u003e ),\n        defaults =\u003e {\n            controller =\u003e 'blog',\n            action     =\u003e 'show_date',\n        },\n        # validate with ...\n        validations =\u003e {\n            # ... raw-Regexp refs\n            year       =\u003e rx/\\d ** 4/,\n            # ... custom types you created\n            month      =\u003e NumericMonth,\n            # ... anon-subsets created inline\n            day        =\u003e (anon subset NumericDay of Int where * \u003c= 31),\n        }\n    ));\n\n    $router.add-route('blog/:action/?:id' =\u003e %(\n        defaults =\u003e {\n            controller =\u003e 'blog',\n        },\n        validations =\u003e {\n            action  =\u003e rx/\\D+/,\n            id      =\u003e Int,  # also use Perl6 types too\n        }\n    ));\n\n    # even include other routers\n    $router.include-router( 'polls/' =\u003e $another_router );\n\n    # ... in your dispatcher\n\n    # returns a Path::Router::Route::Match object\n    my $match = $router.match('/blog/edit/15', context =\u003e %( method =\u003e 'GET' ));\n\n    # ... in your code\n\n    my $uri = $router.path-for(\n        controller =\u003e 'blog',\n        action     =\u003e 'show_date',\n        year       =\u003e 2006,\n        month      =\u003e 10,\n        day        =\u003e 5,\n    );\n\nDESCRIPTION\n===========\n\nThis module provides a way of deconstructing paths into parameters suitable for dispatching on. It also provides the inverse in that it will take a list of parameters, and construct an appropriate uri for it.\n\nReversable\n----------\n\nThis module places a high degree of importance on reversability. The value produced by a path match can be passed back in and you will get the same path you originally put in. The result of this is that it removes ambiguity and therefore reduces the number of possible mis-routings.\n\nVerifiable\n----------\n\nThis module also provides additional tools you can use to test and verify the integrity of your router. These include:\n\n  * An interactive shell in which you can test various paths and see the match it will return, and also test the reversability of that match.\n\n  * A [Test::Path::Router](Test::Path::Router) module which can be used in your applications test suite to easily verify the integrity of your paths.\n\nValidated and Automatically Coerced\n-----------------------------------\n\nEach path may use one or more variables, each given a validation. If a numeric type is used, the value passed on to the action will also be coerced into the correct value.\n\nFlexible\n--------\n\nThis module has no opinions about what it might be useful for. It simply produces a hash of values that can be used for dispatch, logging, or whatever your application is.\n\nATTRIBUTES\n==========\n\nroutes\n------\n\n    has Path::Router::Route @.routes\n\nStores all the route objects that have been added to the router.\n\nMETHODS\n=======\n\nmethod add-route\n----------------\n\n    method add-route(Str $path, *%options --\u003e Int)\n\nAdds a new route to the *end* of the routes list.\n\nReturns the number of routes stored.\n\nmethod insert-route\n-------------------\n\n    method insert-route(Str $path, *%options --\u003e Int)\n\nAdds a new route to the routes list. You may specify an `at` parameter, which would indicate the position where you want to insert your newly created route. The `at` parameter is the `index` position in the list, so it starts at 0.\n\nReturns the number of routes stored.\n\nExamples:\n\n    # You have more than three paths, insert a new route at\n    # the 4th item\n    $router.insert-route($path =\u003e %(\n        at =\u003e 3, |%options\n    ));\n\n    # If you have less items than the index, then it's the same as\n    # as add_route -- it's just appended to the end of the list\n    $router.insert-route($path =\u003e %(\n        at =\u003e 1_000_000, |%options\n    ));\n\n    # If you want to prepend, omit \"at\", or specify 0\n    $router.insert-route($path =\u003e %(\n        at =\u003e 0, |%options\n    ));\n\nmethod include-router\n---------------------\n\n    method include-router (Str $path, Path::Router $other-router --\u003e Int)\n\nThis extracts all the route from `$other-router` and includes them into the invocant router and prepends `$path` to all their paths.\n\nIt 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.\n\nReturns the number of routes stored.\n\nmethod match\n------------\n\n    method match(Str $path, :%context --\u003e Path::Router::Route::Match)\n\nReturn a [Path::Router::Route::Match](Path::Router::Route::Match) object for the best route that matches the given the `$path` and `%context` (if given), or an undefined type-object if no routes match.\n\nThe `%context` is an optional value that is only used if routes with conditions are present. The context is used as an additional match in the process and can be used to apply extra conditions, such as matching the HTTP method when used in a web application.\n\nThe \"best route\" is chosen by first matching the `$path` against every route and then applying the following rules:\n\nover\n====\n\n\n\n  * If no route matches, an undefined type object will be returned. If exactly one route matches, a match for that route will be returned.\n\n  * If multiple routes match, the one with the most required variables will be considered the best match and be returned.\n\n  * In the case that exactly two routes match and have the same number of variables, but one has conditions and the other does not, the one that has conditions will be considered best and returned.\n\n  * Otherwise, if there is more than one matching route with the same number of required variables, an [X::Path::Router::AmbiguousMatch::PathMatch](#X::Path::Router::AmbiguousMatch::PathMatch) exception is thrown. This exception contains all the best matches, so your code can disambiguate them in any way you want or treat this as an error condition as suits your application.\n\nmethod path-for\n---------------\n\n    method path-for(:%context, *%path_descriptor --\u003e Str)\n\nFind the path that, when passed to `method match `, would produce the given arguments. Returns the path without any leading `/`. Returns an undefined type-object if no routes match.\n\nThe `%context` is optional, but if present, this will also apply any route conditions to the given `%context`.\n\nThis will throw an [X::Path::Router::AmbiguousMatch::ReverseMatch](#X::Path::Router::AmbiguousMatch::ReverseMatch) exception if multiple URLs match. This exception includes the possible routes so your code can disambiguate them in whatever fashion makes sense to you.\n\nDEBUGGING\n=========\n\nYou can turn on the verbose debug logging with the `PATH_ROUTER_DEBUG` environment variable. Set that environment variable to a truthy value to enable debugging.\n\nDIAGNOSTIC\n==========\n\nX::Path::Router\n---------------\n\nAll path router exceptions inherit from this exception class.\n\nX::Path::Router::AmbiguousMatch::PathMatch\n------------------------------------------\n\nThis exception is thrown when a path is found to match two different routes equally well.\n\nProvides:\n\n  * `method path(--\u003e Str) ` returns the ambiguous path.\n\n  * `method matches(--\u003e Array) ` returns the best matches found.\n\nX::Path::Router::AmbiguousMatch::ReverseMatch\n---------------------------------------------\n\nThis exception is thrown when two paths are found to match a given criteria when looking up the `path-for` a path\n\nProvides:\n\n  * `method match-keys(--\u003e Array[Str]) ` returns the mapping that was ambiguous\n\n  * `method routes(--\u003e Array[Str]) ` returns the best matches found\n\nX::Path::Router::BadInclusion\n-----------------------------\n\nThis exception is thrown whenever an attempt is made to include one router in another incorrectly.\n\nX::Path::Router::BadRoute\n-------------------------\n\nThis exception is thrown when a route has some serious flaw.\n\nProvides:\n\n  * `method path(--\u003e Str) ` returns the bad route\n\nX::Path::Router::BadValidation\n------------------------------\n\nThis is an [X::Path::Router::BadRoute](#X::Path::Router::BadRoute) exception that is thrown when a validation for a variable that is not found in the path.\n\nProvides:\n\n  * `method validation(--\u003e Str) ` returns the validation variable that was named in the route, but was not found in the path\n\nX::Path::Router::BadSlurpy\n--------------------------\n\nThis is an [X::Path::Router::BadRoute](#X::Path::Router::BadRoute) exception that is thrown when a validation attempts to add a slurpy parameter that is not at the end of the path.\n\nAUTHOR\n======\n\nAndrew Sterling Hanenkamp lthanenkamp@cpan.orggt\n\nBased very closely on the original Perl 5 version by Stevan Little ltstevan.little@iinteractive.comgt\n\nCOPYRIGHT\n=========\n\nCopyright 2015 Andrew Sterling Hanenkamp.\n\nLICENSE\n=======\n\nThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzostay%2Fraku-path-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzostay%2Fraku-path-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzostay%2Fraku-path-router/lists"}