{"id":28317943,"url":"https://github.com/funkybunch/simple-http-router","last_synced_at":"2025-06-24T15:31:52.057Z","repository":{"id":62507972,"uuid":"169944111","full_name":"funkybunch/Simple-HTTP-Router","owner":"funkybunch","description":"A Simple Server-Side HTTP Request Router written in PHP.  This was built to be intended for APIs but will work for Web UI traffic as well.","archived":false,"fork":false,"pushed_at":"2020-02-24T05:03:38.000Z","size":16,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-01T14:21:27.670Z","etag":null,"topics":["api","api-router","http","http-requests","php-router","php-routing","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/funkybunch.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}},"created_at":"2019-02-10T04:51:35.000Z","updated_at":"2020-02-24T05:03:40.000Z","dependencies_parsed_at":"2022-11-02T13:16:16.112Z","dependency_job_id":null,"html_url":"https://github.com/funkybunch/Simple-HTTP-Router","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/funkybunch/Simple-HTTP-Router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funkybunch%2FSimple-HTTP-Router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funkybunch%2FSimple-HTTP-Router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funkybunch%2FSimple-HTTP-Router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funkybunch%2FSimple-HTTP-Router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/funkybunch","download_url":"https://codeload.github.com/funkybunch/Simple-HTTP-Router/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/funkybunch%2FSimple-HTTP-Router/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261703047,"owners_count":23196884,"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","api-router","http","http-requests","php-router","php-routing","router"],"created_at":"2025-05-25T06:14:40.645Z","updated_at":"2025-06-24T15:31:52.044Z","avatar_url":"https://github.com/funkybunch.png","language":"PHP","readme":"*2020 Update:* PHP will forever have a special place in my heart, but its 2020 and I have moved onto other projects.  As such this repository will no longer be maintained or updated.  This repo and code will remain available for anyone to use, fork, modify to your heart's content, but should be considered \"No longer supported\".\n\n# Simple HTTP Router\n[![Build Status](https://travis-ci.org/funkybunch/Simple-HTTP-Router.svg?branch=master)](https://travis-ci.org/funkybunch/Simple-HTTP-Router)\n\nA simple router for HTTP requests written in PHP.  Simple HTTP Router allows you to have a single point of entry for your web application or API and explicitly call different functions based on the HTTP `REQUEST_METHOD` and `REQUEST_URI`.\n\n## Installation\n#### Simplest (Composer)\nThe quickest way to get up and running is to use [Composer](https://github.com/composer/composer).\n\n1. Once you have Composer installed, initialize it in your root project directory using:\n    ```sh \n    $ composer init\n    ```\n2. Now you're all ready to start adding some dependencies.  To install Simple HTTP Router:\n    ```sh\n    $ composer require funkybunch/simple-http-router\n    ```\n\nThat's it!  Just make sure to `autoload` at the top of your code.\n```php\n\u003c?php\nrequire 'vendor/autoload.php';\n```\n\n#### Complex (Manual)\nDownload the files in the repo and navigate to the `src/` directory.  These are the files you will need to `include` or `require` to get going.\n```sh\nsrc/\n |- HTTPErrors.php\n |- Request.php\n |- Router.php\n```\n\nYou will need to include all 3 since they use a `namespace` and do not reference each other directly.\n\nKeep in mind that composer will **not** be able to automatically update this dependency if you do a manual installation so it is highly recommended that you use Composer instead.\n\n\n## Usage\nOnce you are all setup and have everything installed, create the file you want to use as your web app/api facing file.  Typically, this will be an `index.php` file in your web server live directory root.  We'll refer to this as your `router` file.\n\n#### Create the `Router()`\nTo get started, you need to create a new `Router()` object:\n```php\n$router = new FunkyBunch\\SimpleHTTPRouter\\Router;\n```\n\n#### `GET` Request\nTo setup a route for a `GET` request, use the `get()` method in the `Router()` class.  This method requires the `$path` that you are setting the route for, as well as an anonymous `function()`.  This `function()` is called if the `$path` matches the HTTP request.\n\nYou can add as many `get()` methods as you need.\n```php\n$router-\u003eget($path, function(){});\n```\n\n##### Here's what your `index.php` file might look like at this point:\n```php\n\u003c?php\n/**\n * HTTP Router for myWebApp\n * @author You!\n */\n\n$router = new FunkyBunch\\SimpleHTTPRouter\\Router;\n\n$router-\u003eget('/', function() {\n    // The following code will be executed when this `route` is called.\n    echo \"\u003ch1\u003eHello world\u003c/h1\u003e\";\n});\n\n$router-\u003eget('/about', function() {\n    // The following code will be executed when this `route` is called.\n    echo \"\u003ch1\u003eAbout Us\u003c/h1\u003e\";\n});\n```\n\n#### `POST` Request\nTo setup a route for a `POST` request, use the `post()` method in the `Router()` class.  Similarly to the `get()` method, this method requires the `$path` that you are setting the route for, as well as an anonymous `function()`.  This `function()` is called if the `$path` matches the HTTP request.\n```php\n$router-\u003epost($path, function(){});\n```\n##### Here's what your `index.php` file might look like at this point:\n```php\n\u003c?php\n/**\n * HTTP Router for myWebApp\n * @author You!\n */\n\n$router = new FunkyBunch\\SimpleHTTPRouter\\Router;\n\n$router-\u003eget('/', function() {\n    // The following code will be executed when this `route` is called.\n    echo \"\u003ch1\u003eHello world\u003c/h1\u003e\";\n});\n\n$router-\u003eget('/about', function() {\n    // The following code will be executed when this `route` is called.\n    echo \"\u003ch1\u003eAbout Us\u003c/h1\u003e\";\n});\n\n$router-\u003epost('/api/contact', function() {\n    // The following code will be executed when this `route` is called.\n    // Handle `POST` data\n});\n```\n\n#### `getRequest()` Method\nIf you are building an API, you will likely need to get the `body` of the HTTP request.  This can be done using the `getRequest()` method.  This returns the request object for both `GET` and `POST` requests.\n```php\n$router-\u003egetRequest()\n```\n\nIt is possible to use this method inside the `callback` function for either the `get()` or `post()` methods using a [closure](http://php.net/closure).  For example:\n```php\n$router-\u003epost($path, function() use(\u0026$router) {\n    $request = $router-\u003egetRequest();\n    // DO SOMETHING WITH $request\n}\n```\nThe `\u0026` is important because it tells PHP to use the global variable that you have already defined.\n\n##### Here's what your `index.php` file might look like at this point:\n```php\n\u003c?php\n/**\n * HTTP Router for myWebApp\n * @author You!\n */\n\n$router = new FunkyBunch\\SimpleHTTPRouter\\Router;\n\n$router-\u003eget('/', function() {\n    // The following code will be executed when this `route` is called.\n    echo \"\u003ch1\u003eHello world\u003c/h1\u003e\";\n});\n\n$router-\u003eget('/about', function() {\n    // The following code will be executed when this `route` is called.\n    echo \"\u003ch1\u003eAbout Us\u003c/h1\u003e\";\n});\n\n$router-\u003epost('/api/contact', function() use(\u0026$router) {\n    // The following code will be executed when this `route` is called.\n    $request = $router-\u003egetRequest();\n    \n    // DO SOMETHING WITH $request to read POST data\n});\n```\n\n**Note:** Even if Apache, or your favorite web server, is serving `/var/www/mywebapp/web/` then it can still reference files in `/var/www/mywebapp/`.  This way you can keep the rest of your application files out of a live web directory.  The important part is that the file using the router is in the live directory.\n\n## Contributing \u0026 Bugs\nContributions are always welcome!  Draft a [pull request](https://github.com/funkybunch/Simple-HTTP-Router/pulls) with any changes or improvements you make and submit it for review.\n\nFor bug reports, please create an [issue](https://github.com/funkybunch/Simple-HTTP-Router/issues) and I will look into it further.\n\n## Roadmap\n- Advanced Error States \u0026 Handling\n\nPlease create an [issue](https://github.com/funkybunch/Simple-HTTP-Router/issues) for any other suggestions.\n\n## License\nCopyright (c) 2019 Mark Adkins and other [contributors](https://github.com/funkybunch/Simple-HTTP-Router/graphs/contributors).  This project is Open Source and licensed under the [MIT License](https://github.com/funkybunch/Simple-HTTP-Router/blob/master/LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffunkybunch%2Fsimple-http-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffunkybunch%2Fsimple-http-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffunkybunch%2Fsimple-http-router/lists"}