{"id":16024822,"url":"https://github.com/joseluisq/ruta","last_synced_at":"2025-03-17T21:30:38.289Z","repository":{"id":57001676,"uuid":"364957640","full_name":"joseluisq/ruta","owner":"joseluisq","description":"A lightweight HTTP routing library for PHP 8+ without external dependencies. (WIP)","archived":false,"fork":false,"pushed_at":"2023-11-26T22:58:01.000Z","size":147,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-09T23:01:53.708Z","etag":null,"topics":["http","http-router","php","php-library","php8","routing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joseluisq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-05-06T15:32:34.000Z","updated_at":"2022-11-09T01:37:35.000Z","dependencies_parsed_at":"2023-12-03T20:41:40.291Z","dependency_job_id":null,"html_url":"https://github.com/joseluisq/ruta","commit_stats":{"total_commits":60,"total_committers":2,"mean_commits":30.0,"dds":0.01666666666666672,"last_synced_commit":"7fd5733933e2e3b4b810ad47a8fb7b3bdd9899da"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseluisq%2Fruta","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseluisq%2Fruta/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseluisq%2Fruta/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseluisq%2Fruta/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joseluisq","download_url":"https://codeload.github.com/joseluisq/ruta/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243741017,"owners_count":20340387,"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":["http","http-router","php","php-library","php8","routing"],"created_at":"2024-10-08T19:40:20.577Z","updated_at":"2025-03-17T21:30:38.259Z","avatar_url":"https://github.com/joseluisq.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ruta\n\n[![tests ci](https://github.com/joseluisq/ruta/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/joseluisq/ruta/actions/workflows/tests.yml) [![Latest Stable Version](https://poser.pugx.org/joseluisq/ruta/version)](https://packagist.org/packages/joseluisq/ruta) [![Latest Unstable Version](https://poser.pugx.org/joseluisq/ruta/v/unstable)](//packagist.org/packages/joseluisq/ruta) [![Total Downloads](https://poser.pugx.org/joseluisq/ruta/downloads)](https://packagist.org/packages/joseluisq/ruta) [![License](https://poser.pugx.org/joseluisq/ruta/license)](https://packagist.org/packages/joseluisq/ruta)\n\n\u003e A lightweight HTTP routing library for PHP. (WIP)\n\n## Features\n\n- [x] HTTP status codes\n- [x] HTTP Methods\n- [x] HTTP Headers\n- [x] **HTTP Requests**\n  - [x] Handle `multipart/form-data` request data\n  - [x] Handle `x-www-form-urlencoded` request data\n  - [x] Handle `application/xml` request data\n  - [x] Handle `application/json` request data\n  - [x] Handle `query` URI request params\n  - [x] Access request `path` and `headers`\n- [x] **HTTP Responses**\n  - [x] HTML, JSON, XML or Text responses\n  - [x] HTTP redirections\n- [x] **HTTP Routing**\n  - [x] Routes with placeholder arguments: `/abc/{some}`\n  - [x] Handle multiple valid HTTP methods\n  - [x] Handle any valid HTTP method\n  - [x] Handle not match routes (404s)\n  - [x] Support callback or class/method style\n  - [x] Optional and order-insensitive arguments on callback or class/method handlers\n  - [x] Regular expressions support: `/abc/regex(id=^[0-9]+$)`\n  - [ ] Optional route arguments: `/abc/{some?}`\n  - [ ] Fallback routes\n  - [ ] Route's cache\n- [x] **Middlewares**\n  - [x] Inspect or filter valid requests before routing\n  - [ ] Inspect or filter valid requests after routing\n\nSee [Leap](https://github.com/joseluisq/leap) micro-framework based on Ruta.\n\n## Requirements\n\n[PHP 8.0](https://www.php.net/releases/8.0/en.php) or newer.\n\n## Install\n\nInstall via [Composer](https://packagist.org/packages/joseluisq/ruta)\n\n```sh\ncomposer require joseluisq/ruta:dev-master\n```\n\n## Usage\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nrequire 'vendor/autoload.php';\n\nuse Ruta\\Header;\nuse Ruta\\Method;\nuse Ruta\\Ruta;\nuse Ruta\\Request;\nuse Ruta\\Response;\nuse Ruta\\Status;\n\n// 1. Callback style\n\n// NOTE: `Request`, `Response`, `array` (slug arguments) are passed to the callback.\n// However they are optional and their order can be modified. See more examples below.\n\nRuta::get('/home/hola', function (Request $req, Response $res) {\n    $res-\u003ejson([\n        'host' =\u003e $req-\u003eheader(Header::Host),\n        'headers' =\u003e $req-\u003eheaders(),\n    ]);\n});\nRuta::get('/home/hola/redirect', function (Response $res) {\n    $res-\u003eredirect('/home/aaa/some/bbb');\n});\nRuta::get('/reg/regex(id=^[0-9]+$)/exp', function (Response $res, array $args) {\n    $res-\u003ejson(['args' =\u003e $args]);\n});\nRuta::post('/home/{path3}/some2', function (Response $res) {\n    $res-\u003ejson(['post_data' =\u003e 11010101010]);\n});\n\nRuta::some('/home/some', [Method::POST, Method::PUT], function (Request $req, Response $res) {\n    $res-\u003ejson(['only' =\u003e $req-\u003emethod()]);\n});\n\nRuta::any('/home/methods', function (Request $req, Response $res) {\n    $res-\u003ejson(['method' =\u003e $req-\u003emethod()]);\n});\n\nRuta::post('/home/{path}', function (Response $res) {\n    $res\n        -\u003eheader('X-Header-One', 'Header Value 1')\n        -\u003eheader('X-Header-Two', 'Header Value 2')\n        -\u003ejson(['some_data' =\u003e 223424234]);\n});\n\n// 2. class/method style\nclass HomeCtrl\n{\n    public function index(Request $req, Response $res, array $args)\n    {\n        // 2.1 $args contains route placeholder values\n        if (array_key_exists('path1', $args)) {\n            // do something...\n        }\n\n        // 2.2. Get data provided via `multipart/form-data` \n        $data = $req-\u003emultipart();\n        // 2.3. Get all headers\n        $data = $req-\u003eheaders();\n        // 2.4. Get a single header\n        $data = $req-\u003eheader(\"Host\");\n        // 2.5. Get data provided via `application/x-www-form-urlencoded` \n        $data = $req-\u003eurlencoded();\n        // 2.6. Get data provided via `application/json`\n        $data = $req-\u003ejson();\n        // 2.7. Get data provided via `application/xml`\n        $data = $req-\u003exml();\n        // 2.8. Get query data\n        $data = $req-\u003equery();\n\n        $res-\u003ejson(['data' =\u003e 'Message from a class!']);\n    }\n\n    // Custom 404 reply\n    public function not_found(Response $res)\n    {\n        $res\n            -\u003estatus(Status::NotFound)\n            -\u003etext(\"404 - Page Not Found!\");\n    }\n}\n\nRuta::get('/home/{path1}/some/{path2}', [HomeCtrl::class, 'index']);\n\n// 3. Handle 404 not found routes\nRuta::not_found([HomeCtrl::class, 'not_found']);\n```\n\n## Code example\n\nFile: [example/nginx/public/index.php](example/nginx/public/index.php)\n\n```sh\n# Or run example using Docker + Nginx server\nmake compose-up\n```\n\n```sh\n# Run example using the PHP built-in server\nmake container-dev\n```\n\nNow navigate for example to [http://localhost:8088/home/hola](http://localhost:8088/home/hola)\n\n## Contributions\n\nFeel free to send a [pull request](https://github.com/joseluisq/ruta/pulls) or file some [issue](https://github.com/joseluisq/ruta/issues).\n\n## Contributions\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in current work by you, as defined in the Apache-2.0 license, shall be dual licensed as described below, without any additional terms or conditions.\n\nFeel free to send some [Pull request](https://github.com/joseluisq/ruta/pulls) or [issue](https://github.com/joseluisq/ruta/issues).\n\n## License\n\nThis work is primarily distributed under the terms of both the [MIT license](LICENSE-MIT) and the [Apache License (Version 2.0)](LICENSE-APACHE).\n\n© 2021-present [Jose Quintana](https://git.io/joseluisq)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoseluisq%2Fruta","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoseluisq%2Fruta","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoseluisq%2Fruta/lists"}