{"id":24408582,"url":"https://github.com/jenstornell/tinyrouter","last_synced_at":"2025-04-12T02:04:12.309Z","repository":{"id":217043587,"uuid":"151041761","full_name":"jenstornell/TinyRouter","owner":"jenstornell","description":"TinyRouter is perhaps the smallest PHP router library on earth","archived":false,"fork":false,"pushed_at":"2020-05-09T12:58:07.000Z","size":20,"stargazers_count":7,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T02:04:02.463Z","etag":null,"topics":["php","php-router","php-router-standalone","php-routing","router","routing"],"latest_commit_sha":null,"homepage":"","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/jenstornell.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-10-01T05:45:45.000Z","updated_at":"2023-07-29T02:04:48.000Z","dependencies_parsed_at":"2024-01-14T08:12:19.823Z","dependency_job_id":null,"html_url":"https://github.com/jenstornell/TinyRouter","commit_stats":null,"previous_names":["jenstornell/tinyrouter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenstornell%2FTinyRouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenstornell%2FTinyRouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenstornell%2FTinyRouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenstornell%2FTinyRouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jenstornell","download_url":"https://codeload.github.com/jenstornell/TinyRouter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505863,"owners_count":21115354,"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":["php","php-router","php-router-standalone","php-routing","router","routing"],"created_at":"2025-01-20T05:52:17.751Z","updated_at":"2025-04-12T02:04:12.286Z","avatar_url":"https://github.com/jenstornell.png","language":"PHP","funding_links":["https://www.paypal.me/DevoneraAB"],"categories":[],"sub_categories":[],"readme":"# TinyRouter\n\n*Version: 2.0*\n\nProbably the smallest PHP router library on earth.\n\n## Files\n\n### `.htaccess`\n\nAdd the `.htaccess` (code below).\n\n```htaccess\nOptions -Indexes\nRewriteEngine on\n# RewriteBase /\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^(.*) index.php [L]\n```\n\n### `tinyrouter.php`\n\nAdd the `tinyrouter.php` to a folder (code below).\n\n```php\n\u003c?php\nclass route\n{\n  public static function __callStatic($method, $args)\n  {\n    if ($_SERVER['REQUEST_METHOD'] == strtoupper($method)) {\n\n      $presets = [\n        ':all' =\u003e '.*',\n        ':alpha' =\u003e '[a-zA-Z]+',\n        ':any' =\u003e '[^/]+',\n        ':num' =\u003e '\\d+|-\\d+',\n      ];\n\n      $pattern = $args[0];\n\n      foreach ($presets as $shortcode =\u003e $regex) {\n        $pattern = strtr($pattern, [$shortcode =\u003e '(' . $regex . ')']);\n      }\n      $pattern = '~^' . $pattern . '$~';\n\n      $uri = '/';\n      $dir = dirname($_SERVER['SCRIPT_NAME']);\n\n      if (isset($_SERVER['REDIRECT_URL']) \u0026\u0026 $dir != '/') {\n        $uri = strtr($_SERVER['REDIRECT_URL'], [$dir =\u003e '']);\n      }\n\n      preg_match($pattern, $uri, $matches);\n\n      if (!$matches) return;\n\n      $output = $args[1]($matches);\n      if (isset($output)) die($output);\n    }\n  }\n}\n\nfunction route($pattern, $method)\n{\n  route::get($pattern, $method);\n}\n```\n\n## Usage\n\n```php\ninclude __DIR__ . '/tinyrouter.php';\n\nroute::post('form/:any', function($matches) {\n  // Will only run with POST requests. You can PUT or whatever.\n});\n\nroute(':all', function($matches) {\n  // Will only run with GET requests\n});\n\n// When no match is found it will fall down here\nheader(\"HTTP/1.0 404 Not Found\");\ndie('Error 404 - No route could be found');\n```\n\n## Patterns\n\n### Predefined patterns\n\n- `:all` matches everyting from current position until the end `.*`\n- `:alpha` matches any alphabetically character `[a-zA-Z]+`\n- `:any` matches anything within slashes `[^/]+`\n- `:num` matches any number, even negative ones `\\d+|-\\d+`\n\nThe below will match for examlpe `blog/2010/01/my-story`.\n\n```php\nroute('blog/:num/:num/:any', function($matches) {\n  // Return something\n});\n```\n\n### Custom patterns\n\nThe below will match for examlpe `assets/my/images/picture.jpg`.\n\n```php\nroute('assets/(.*)\\.(jpg|jpeg|png|gif|svg)$', function($matches) {\n  // Return something\n});\n```\n\n## Donate\n\nDonate to [DevoneraAB](https://www.paypal.me/DevoneraAB) if you want.\n\n## Requirements\n\n- PHP 7\n- Rewrite module enabled\n\n## License\n\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenstornell%2Ftinyrouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjenstornell%2Ftinyrouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenstornell%2Ftinyrouter/lists"}