{"id":18904300,"url":"https://github.com/mohdrashid/phprouter","last_synced_at":"2025-07-11T19:34:00.690Z","repository":{"id":143972828,"uuid":"84721008","full_name":"mohdrashid/PHPRouter","owner":"mohdrashid","description":"An express inspired Routing library for PHP","archived":false,"fork":false,"pushed_at":"2019-09-29T08:54:02.000Z","size":1118,"stargazers_count":24,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-15T03:51:20.724Z","etag":null,"topics":["api","http","php","php7","router","routing"],"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/mohdrashid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","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,"zenodo":null}},"created_at":"2017-03-12T11:40:25.000Z","updated_at":"2025-04-10T10:04:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"34a3679e-6f52-4b41-a8f2-00311a160067","html_url":"https://github.com/mohdrashid/PHPRouter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mohdrashid/PHPRouter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohdrashid%2FPHPRouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohdrashid%2FPHPRouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohdrashid%2FPHPRouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohdrashid%2FPHPRouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mohdrashid","download_url":"https://codeload.github.com/mohdrashid/PHPRouter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohdrashid%2FPHPRouter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264886920,"owners_count":23678602,"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":["api","http","php","php7","router","routing"],"created_at":"2024-11-08T09:08:01.601Z","updated_at":"2025-07-11T19:34:00.658Z","avatar_url":"https://github.com/mohdrashid.png","language":"PHP","readme":"PHPRouter is a simple routing extension for PHP inspired by express framework in Node.JS.\r\n\r\n\u003ca name=\"install\"\u003e\u003c/a\u003e\r\n## Installation\r\nDownload the zip folder and extract to the directory where your project is located in.\r\n\r\n\r\n\u003ca name=\"usage\"\u003e\u003c/a\u003e\r\n## Usage\r\nInclude the router in the project like\r\n\r\n```php\r\ninclude_once('PHPRouter/router.php');\r\n```\r\n\r\nPHP router supports \u003cb\u003eGET\u003c/b\u003e, \u003cb\u003ePOST\u003c/b\u003e, \u003cb\u003ePUT\u003c/b\u003e, \u003cb\u003eDELETE\u003c/b\u003e, \u003cb\u003ePATCH\u003c/b\u003e and \u003cb\u003eANY\u003c/b\u003erequests. Function \u003cb\u003eany\u003c/b\u003e can be used to capture all events. The callback will return request parameters and response object in order.\r\n\r\nA typical route looks initialization looks like:\r\n```php\r\n//where method is get,post,put,delete, patch or any\r\n$app-\u003emethod('/', function($request,$response){\r\n  $response-\u003esend(\"GET request\");\r\n});\r\n```\r\n\r\nSupport for complex routes using regex\r\n```php\r\n//where method is get,post,put,delete or patch\r\n$app-\u003emethod('/:id', function($request,$response){\r\n  $response-\u003esend(\"GET request\");\r\n});\r\n```\r\nHere the id will be returned as name-value pair in request array's \"params\" field.\r\n\r\n-------------------------------------------------------------------\r\n\u003ca name=\"example\"\u003e\u003c/a\u003e\r\n## Example\r\n\r\n```php\r\n\u003c?php\r\nrequire_once('../PHPRouter/router.php');\r\n//Initalizing the PHPRouter class\r\n$app = new PHPRouter\\Router();\r\n/*****\r\nRoutes\r\n******/\r\n//All HTTP request for /employee will come here\r\n$app-\u003eany('/employee', function($request,$response){\r\n  $response-\u003esend(\"ANY request\");\r\n});\r\n//All GET request for /user will come here\r\n$app-\u003eget('/user', function($request,$response){\r\n  $response-\u003ejson([\"id\"=\u003e\"1\",\"name\"=\u003e\"DroidHat\",\"url\"=\u003e\"http://www.droidhat.com\"],200);\r\n});\r\n//All POST request for /:id will come here; where id is any alphanumeral\r\n$app-\u003epost('/:id', function($request,$response){\r\n  $response-\u003ejson([\"id\"=\u003e($request[\"params\"][\"id\"])],200);\r\n});\r\n//All POST request for /:id/:name will come here; where id and name are any alphanumeral\r\n$app-\u003epost('/:id/:name', function($request,$response){\r\n  $response-\u003ejson([\"id\"=\u003e($request[\"params\"][\"id\"]),\"name\"=\u003e($request[\"params\"][\"name\"])],200);\r\n});\r\n$app-\u003eput('/', function($request,$response){\r\n  $response-\u003esend(\"PUT request\");\r\n});\r\n$app-\u003epatch('/', function($request,$response){\r\n  $response-\u003esend(\"PATCH request\");\r\n});\r\n$app-\u003edelete('/', function($request,$response){\r\n  $response-\u003esend(\"DELETE request\");\r\n});\r\n//Error Handler\r\n$app-\u003eerror(function(Exception $e,$response){\r\n  $response-\u003esend('path not found',404);\r\n});\r\n//Starting the router\r\n$app-\u003estart();\r\n?\u003e\r\n```\r\n\u003ca name=\"api\"\u003e\u003c/a\u003e\r\n## API\r\n\r\n  * \u003ca href=\"#method\"\u003e\u003ccode\u003e$app-\u003e\u003cb\u003eget()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\r\n  * \u003ca href=\"#method\"\u003e\u003ccode\u003e$app-\u003e\u003cb\u003epost()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\r\n  * \u003ca href=\"#method\"\u003e\u003ccode\u003e$app-\u003e\u003cb\u003eput()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\r\n  * \u003ca href=\"#method\"\u003e\u003ccode\u003e$app-\u003e\u003cb\u003edelete()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\r\n  * \u003ca href=\"#method\"\u003e\u003ccode\u003e$app-\u003e\u003cb\u003epatch()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\r\n  * \u003ca href=\"#method\"\u003e\u003ccode\u003e$app-\u003e\u003cb\u003eany()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\r\n  * \u003ca href=\"#error\"\u003e\u003ccode\u003e$app-\u003e\u003cb\u003eerror()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\r\n  * \u003ca href=\"#start\"\u003e\u003ccode\u003e$app-\u003e\u003cb\u003estart()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\r\n\r\n-------------------------------------------------------\r\n\u003ca name=\"method\"\u003e\u003c/a\u003e\r\n### Route Methods\r\n\r\n\u003cb\u003e\u003ccode\u003e$app-\u003emethod($path, function($request,$response){});\u003c/code\u003e\u003c/b\u003e where method is get, post, put, delete, patch or any. any supports all methods.\r\n\r\nThe first parameter is the route path like '/' or '/user'.\r\nSecond parameter is the callback function, i.e., the function to be called when processing is done.\r\nThe callback function takes \u003ca href=\"#request\"\u003erequest\u003c/a\u003e array and \u003ca href=\"#response\"\u003eresponse\u003c/a\u003e object as parameters.\r\n\r\n---------------------------------------------------------\r\n\u003ca name=\"request\"\u003e\u003c/a\u003e\r\n### Request Array\r\n\r\nContains headers(HTTP information, Request information and PHP_AUTH) information, body parameter, url parameters, files and cookies informations in array format.\r\n\r\n\u003cbr\u003e\r\n\u003cb\u003eArray indexes\u003c/b\u003e\u003cbr\u003e\r\n\u003cb\u003e\"raw\"\u003c/b\u003e: Body in raw format\u003cbr\u003e\r\n\u003cb\u003e\"body\"\u003c/b\u003e: Body in associative array format like $_POST\u003cbr\u003e\r\n\u003cb\u003e\"header\"\u003c/b\u003e: HTTP,REQUEST and PHP_AUTH information\u003cbr\u003e\r\n\u003cb\u003e\"method\"\u003c/b\u003e: The type of HTTP REQUEST\u003cbr\u003e\r\n\u003cb\u003e\"params\"\u003c/b\u003e: URL parameters like $_GET\u003cbr\u003e\r\n\u003cb\u003e\"files\"\u003c/b\u003e: FIles if any available like $_FILES\u003cbr\u003e\r\n\u003cb\u003e\"cookies\"\u003c/b\u003e: Cookies if any available like $_COOKIE\u003cbr\u003e\r\n\r\n\u003cb\u003eUsage\u003c/b\u003e\u003cbr\u003e\r\n$request[\"body\"] to access body parameters\r\n\r\n\r\n-------------------------------------------------------\r\n\u003ca name=\"response\"\u003e\u003c/a\u003e\r\n### Response Object\r\n\r\nresponse is an object of class Response. It has methods such as send, json and status.\r\n\r\n\u003cbr\u003e\r\n\u003cb\u003eFucntions\u003c/b\u003e\u003cbr\u003e\r\n1. \u003ccode\u003e\u003cb\u003esend($message,$status)\u003c/b\u003e\u003c/code\u003e: $message is the message that you want to output to the requester and $status is an optional field in case you want to send status also.\u003cbr\u003e\r\n2. \u003ccode\u003e\u003cb\u003ejson($message,$status)\u003c/b\u003e\u003c/code\u003e: $message is the message that you want to output to the requester and $status is an optional field in case you want to send status also. The difference is that here $message should be a PHP array that will be converted the function to JSON.\u003cbr\u003e\r\n3. \u003ccode\u003e\u003cb\u003estatus($status)\u003c/b\u003e\u003c/code\u003e: send HTTP status only.\r\n\r\n\u003cb\u003eUsage\u003c/b\u003e\u003cbr\u003e\r\n$response-\u003esend(\"Hello World\",200); to output to the requester \"hello\" world with a status of \u003cb\u003e200\u003c/b\u003e\u003cbr\u003e\r\n\r\n-------------------------------------------------------\r\n\u003ca name=\"error\"\u003e\u003c/a\u003e\r\n### Error\r\n\r\n$app-\u003eerror(function(Exception $e){})\r\n\r\nError function takes a callback function as a parameter. The callback function will be passed exception information if any occurs.\u003cbr\u003e\r\n\r\n---------------------------------------------------------\r\n\u003ca name=\"start\"\u003e\u003c/a\u003e\r\n### Start Routing\r\n\r\n$app-\u003estart();\r\n\r\nStart the routing process.\u003cbr\u003e\r\n\r\n-------------------------------------------------------\r\n\u003ca name=\"license\"\u003e\u003c/a\u003e\r\n## License\r\n\r\nMIT\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohdrashid%2Fphprouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohdrashid%2Fphprouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohdrashid%2Fphprouter/lists"}