{"id":18771021,"url":"https://github.com/rareloop/wp-router","last_synced_at":"2025-04-13T07:32:22.719Z","repository":{"id":62533412,"uuid":"97320908","full_name":"Rareloop/wp-router","owner":"Rareloop","description":"WordPress Router","archived":false,"fork":false,"pushed_at":"2018-06-05T08:39:26.000Z","size":12,"stargazers_count":15,"open_issues_count":2,"forks_count":2,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-27T23:59:55.041Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Rareloop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-15T14:25:46.000Z","updated_at":"2023-03-09T14:43:21.000Z","dependencies_parsed_at":"2022-11-02T15:00:25.282Z","dependency_job_id":null,"html_url":"https://github.com/Rareloop/wp-router","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rareloop%2Fwp-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rareloop%2Fwp-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rareloop%2Fwp-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rareloop%2Fwp-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rareloop","download_url":"https://codeload.github.com/Rareloop/wp-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223574834,"owners_count":17167569,"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-07T19:22:53.053Z","updated_at":"2024-11-07T19:22:53.708Z","avatar_url":"https://github.com/Rareloop.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"**This package is no longer supported. Use at your own risk. We recommend using the underlying router: https://github.com/Rareloop/router**\n\n# Rare WordPress Router\n![CI](https://travis-ci.org/Rareloop/wp-router.svg?branch=master)\n\nA WordPress wrapper around the [Rareloop PHP Router](https://github.com/rareloop/router). Easily handle custom endpoints on your WordPress site with this plugin.\n\n## Installation\n\nAlthough not a requirement, using Composer and a setup like [Bedrock](https://roots.io/bedrock/) is the recommended installation method.\n\n```\ncomposer require rareloop/wp-router\n```\n\n## Usage\n\n### Creating Routes\n\n#### Map\n\nCreating a route is done using the `map` function:\n\n```php\nuse Rareloop\\WordPress\\Router\\Router;\n\n// Creates a route that matches the uri `/posts/list` both GET \n// and POST requests. \nRouter::map(['GET', 'POST'], 'posts/list', function () {\n    return 'Hello World';\n});\n```\n\n`map()` takes 3 parameters:\n\n- `methods` (array): list of matching request methods, valid values:\n    + `GET`\n    + `POST`\n    + `PUT`\n    + `PATCH`\n    + `DELETE`\n    + `OPTIONS`\n- `uri` (string): The URI to match against\n- `action`  (function|string): Either a closure or a Controller string\n\n#### Route Parameters\nParameters can be defined on routes using the `{keyName}` syntax. When a route matches that contains parameters, an instance of the `RouteParams` object is passed to the action.\n\n```php\nRouter::map(['GET'], 'posts/{id}', function(RouteParams $params) {\n    return $params-\u003eid;\n});\n```\n\n#### Named Routes\nRoutes can be named so that their URL can be generated programatically:\n\n```php\nRouter::map(['GET'], 'posts/all', function () {})-\u003ename('posts.index');\n\n$url = Router::url('posts.index');\n```\n\nIf the route requires parameters you can be pass an associative array as a second parameter:\n\n```php\nRouter::map(['GET'], 'posts/{id}', function () {})-\u003ename('posts.show');\n\n$url = Router::url('posts.show', ['id' =\u003e 123]);\n```\n\n#### HTTP Verb Shortcuts\nTypically you only need to allow one HTTP verb for a route, for these cases the following shortcuts can be used:\n\n```php\nRouter::get('test/route', function () {});\nRouter::post('test/route', function () {});\nRouter::put('test/route', function () {});\nRouter::patch('test/route', function () {});\nRouter::delete('test/route', function () {});\nRouter::options('test/route', function () {});\n```\n\n#### Setting the basepath\nThe router assumes you're working from the route of a domain. If this is not the case you can set the base path:\n\n```php\nRouter::setBasePath('base/path');\nRouter::map(['GET'], 'route/uri', function () {}); // `/base/path/route/uri`\n```\n\n#### Controllers\nIf you'd rather use a class to group related route actions together you can pass a Controller String to `map()` instead of a closure. The string takes the format `{name of class}@{name of method}`. It is important that you use the complete namespace with the class name.\n\nExample:\n\n```php\n// TestController.php\nnamespace \\MyNamespace;\n\nclass TestController\n{\n    public function testMethod()\n    {\n        return 'Hello World';\n    }\n}\n\n// routes.php\nRouter::map(['GET'], 'route/uri', '\\MyNamespace\\TestController@testMethod');\n```\n\n### Creating Groups\nIt is common to group similar routes behind a common prefix. This can be achieved using Route Groups:\n\n```php\nRouter::group('prefix', function ($group) {\n    $group-\u003emap(['GET'], 'route1', function () {}); // `/prefix/route1`\n    $group-\u003emap(['GET'], 'route2', function () {}); // `/prefix/route2§`\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frareloop%2Fwp-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frareloop%2Fwp-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frareloop%2Fwp-router/lists"}