{"id":17369644,"url":"https://github.com/jblond/php-router","last_synced_at":"2026-02-08T12:01:05.613Z","repository":{"id":56997248,"uuid":"78113318","full_name":"JBlond/php-router","owner":"JBlond","description":"lightweight, simpleto use php router","archived":false,"fork":false,"pushed_at":"2024-10-28T15:18:16.000Z","size":59,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-31T11:31:50.232Z","etag":null,"topics":["php","php-router","router"],"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/JBlond.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-05T13:01:52.000Z","updated_at":"2024-11-18T11:40:43.000Z","dependencies_parsed_at":"2024-10-28T21:30:38.373Z","dependency_job_id":"94d27987-0461-4e86-a804-fa6511d9c875","html_url":"https://github.com/JBlond/php-router","commit_stats":{"total_commits":82,"total_committers":2,"mean_commits":41.0,"dds":0.03658536585365857,"last_synced_commit":"00f11c8b143d3894d67ea80007f089c503692d1c"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/JBlond/php-router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlond%2Fphp-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlond%2Fphp-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlond%2Fphp-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlond%2Fphp-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JBlond","download_url":"https://codeload.github.com/JBlond/php-router/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBlond%2Fphp-router/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29229354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-08T09:43:19.170Z","status":"ssl_error","status_checked_at":"2026-02-08T09:42:55.556Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","router"],"created_at":"2024-10-16T00:06:02.935Z","updated_at":"2026-02-08T12:01:05.575Z","avatar_url":"https://github.com/JBlond.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"PHP router\n======================\n\n[![Code Climate](https://codeclimate.com/github/JBlond/php-router/badges/gpa.svg)](https://codeclimate.com/github/JBlond/php-router) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/a89005f98a484c2db2baa832c5bd573b)](https://www.codacy.com/app/leet31337/php-router?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=JBlond/php-router\u0026amp;utm_campaign=Badge_Grade)  \n\n```bash\ncomposer require jblond/php-router\n```\n\n## A simple PHP router class\n\nLess than 300 lines of code with comments\n\nSupports  \n- lambda URLs: \n\t- **:any** Any sign\n\t- **:num** Only numbers\n\t- **:all** All characters\n\t- **:an** A-Z a-z 0-9\n\t- **:url** A-Z a-z 0-9 - _ \n\t- **:hex** hexadecimal \n- Regex URLs e.g. /user/(.*)/edit\n- Optional Subroutes\n- Works in Subdirs, if you use `-\u003eset_basepath('/yoursubdir')` \n- Custom response headers\n\t- download\n\t- header(s)\n\t- redirect\n\t- 503 error\n\n## Install\n\n```\ncomposer require jblond/php-router\n```\n\n## Examples\n\n### Static routes\n```PHP\nrequire 'jblond/Autoloader.php';\nnew \\jblond\\Autoloader();\n$router = new \\jblond\\router\\Router();\n$router-\u003esetBasepath('');\n$router-\u003einit();\n\n$router-\u003eadd('/', function () {\n    echo 'Welcome';\n});\n\n$router-\u003eadd('/info/', function () {\n    phpinfo();\n});\n\n$router-\u003eadd('/test.html', function () {\n    echo 'test.html Welcome';\n});\n$router-\u003eadd('/post/', function () {\n\trequire 'post.html';\n});\n\n$router-\u003epost('/post/reciver/', function () {\n    print_r($_POST);\n});\n$router-\u003erun();\n```\n\n### dynamic routes\n```PHP\t\n$router-\u003eadd('/user/(.*)/edit', function ($id) {\n    echo 'Edit user with id ' . $id;\n});\n\n$router-\u003eget('/test/(:any)', function () {\n    print_r(filter_input(INPUT_SERVER, 'REQUEST_URI'));\n});\n\t\n```\n\n### Integration with other libraries aka using closure\n```PHP\n$tpl = new \\Acme\\Template\\Template();\n$router-\u003eadd('/closure', function () use ($tpl) {\n    // $tpl-\u003e...\n    echo 'closure';\n});\n```\n\n### Define 404 not found page\t\n\t\n```PHP\n$router-\u003eadd404(function ($url) {\n    header(\"HTTP/1.0 404 Not Found\");\n    echo '404 :-( ' . $url;\n});\n$router-\u003erun();\n```\n\n### Responses\n```PHP\n$router-\u003eget('/503/', function (){\n    $response = new \\jblond\\router\\Responses();\n    $response-\u003eerror503();\n});\n```\n\n### Optional Subpatterns\nOptional route subpattern can be made of using `?`  aftern the normal pattern.\n\n```PHP\n$router-\u003eget(\n    '/phonebook(\\/[A-Za-z]+(\\/[A-Za-z]+(\\/[A-Za-z]+(\\/[0-9-]+)?)?)?)?/',\n    function ($lastname = null, $surname = null, $street = null, $number = null) {\n        if(!$lastname) {\n            echo 'Phonebook all entries';\n            return;\n        }\n        if(!$surname){\n            echo 'Phonebook lookup lastname: ' . $lastname;\n            return;\n        }\n        if(!$street){\n            echo 'Phonebook lookup lastname: ' . $lastname . ' Surname: ' . $surname;\n            return;\n        }\n        if(!$number){\n            echo 'Phonebook lookup lastname: ' . $lastname . ' Surname: ' . $surname . ' Street: ' . $street;\n            return;\n        }\n        echo ' FULL SEARCH';\n});\n```\n\nFor the regexpattern see https://regexper.com/#%2Fphonebook(%5C%2F%5BA-Za-z%5D%2B(%5C%2F%5BA-Za-z%5D%2B(%5C%2F%5BA-Za-z%5D%2B(%5C%2F%5B0-9-%5D%2B)%3F)%3F)%3F)%3F%2F\n\nAlso good for testing your regex: http://www.phpliveregex.com/ use preg_match\n\n### Apache rewrite config\n\n```\nRewriteEngine on\nRewriteBase /\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^(.*)$ index.php [QSA,L]\n```\n\nwith apache 2.4 you can use\n\n```\nFallbackResource /index.php\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjblond%2Fphp-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjblond%2Fphp-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjblond%2Fphp-router/lists"}