{"id":16382352,"url":"https://github.com/berliozframework/router","last_synced_at":"2026-03-09T01:32:12.090Z","repository":{"id":56950725,"uuid":"119069775","full_name":"BerliozFramework/Router","owner":"BerliozFramework","description":"Berlioz Router is a PHP library for manage HTTP routes, respecting PSR-7 (HTTP message interfaces) standard.","archived":false,"fork":false,"pushed_at":"2024-12-04T08:56:31.000Z","size":115,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"2.x","last_synced_at":"2025-04-23T13:18:55.081Z","etag":null,"topics":["berlioz","composer","php","php-library","psr","router"],"latest_commit_sha":null,"homepage":"https://getberlioz.com","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/BerliozFramework.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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-01-26T15:41:30.000Z","updated_at":"2024-12-04T08:56:03.000Z","dependencies_parsed_at":"2024-11-04T14:24:03.705Z","dependency_job_id":"4bd34dd0-9a0f-4682-9d09-1e49bb0b1c0f","html_url":"https://github.com/BerliozFramework/Router","commit_stats":{"total_commits":65,"total_committers":1,"mean_commits":65.0,"dds":0.0,"last_synced_commit":"3e6271f327b868d1b403cf897a088d8196b6dcae"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FRouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FRouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FRouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BerliozFramework%2FRouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BerliozFramework","download_url":"https://codeload.github.com/BerliozFramework/Router/tar.gz/refs/heads/2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250439295,"owners_count":21430824,"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":["berlioz","composer","php","php-library","psr","router"],"created_at":"2024-10-11T04:05:04.835Z","updated_at":"2026-03-09T01:32:12.054Z","avatar_url":"https://github.com/BerliozFramework.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Berlioz Router\n\n[![Latest Version](https://img.shields.io/packagist/v/berlioz/router.svg?style=flat-square)](https://github.com/BerliozFramework/Router/releases)\n[![Software license](https://img.shields.io/github/license/BerliozFramework/Router.svg?style=flat-square)](https://github.com/BerliozFramework/Router/blob/2.x/LICENSE)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/BerliozFramework/Router/tests.yml?branch=2.x\u0026style=flat-square)](https://github.com/BerliozFramework/Router/actions/workflows/tests.yml?query=branch%3A2.x)\n[![Quality Grade](https://img.shields.io/codacy/grade/698b7941569c4926b67bab59efbcfafd/2.x.svg?style=flat-square)](https://www.codacy.com/manual/BerliozFramework/Router)\n[![Total Downloads](https://img.shields.io/packagist/dt/berlioz/router.svg?style=flat-square)](https://packagist.org/packages/berlioz/router)\n\n**Berlioz Router** is a PHP library for manage HTTP routes, respecting PSR-7 (HTTP message interfaces) standard.\n\n## Installation\n\n### Composer\n\nYou can install **Berlioz Router** with [Composer](https://getcomposer.org/), it's the recommended installation.\n\n```bash\n$ composer require berlioz/router\n```\n\n### Dependencies\n\n* **PHP** ^8.0\n* Packages:\n    * **berlioz/http-message**\n    * **psr/log**\n\n## Usage\n\n### Routes\n\n#### Create route\n\nYou can create simple route like this:\n\n```php\nuse Berlioz\\Router\\Route;\n\n$route = new Route('/path-of/my-route');\n$route = new Route('/path-of/my-route/{attribute}/with-attribute');\n```\n\nConstructor arguments are:\n\n- **defaults**: an associated array to set default values of attributes when route is generated\n- **requirements**: an associated array to restrict the format of attributes. Key is the name of attribute and value is\n  the validation regex\n- **name**: name of route\n- **method**: an array of allowed HTTP methods, or just a method\n- **host**: an array of allowed hosts, or just a host\n- **priority**: you can specify the priority for a route (default: -1)\n\n#### Route group\n\nA route can be transformed to a group, only associate another route to them.\n\n```php\nuse Berlioz\\Router\\Route;\n\n$route = new Route('/path');\n$route-\u003eaddRoute($route2 = new Route('/path2')); // Path will be: /path/path2\n```\n\nChildren routes inherit parent route attributes, requirements, ...\n\n#### Attributes\n\nRoute accept optional attributes, you need to wrap the optional part by brackets.\n\n```php\n$route = new \\Berlioz\\Router\\Route('/path[/optional-part/{with-attribute}]');\n```\n\nYou can also define requirements directly in the path :\n\n- Add a regular expression after the name of attribute (separate by \":\").\n- Add a type name after the name of attribute (separate by \"::\").\n\n```php\n$route = new \\Berlioz\\Router\\Route('/path/{attributeName:\\d+}');\n$route = new \\Berlioz\\Router\\Route('/path/{attributeName::int}');\n```\n\nSupported defined types:\n\n- `int` (equivalent of `\\d+`)\n- `float` (equivalent of `\\d+(\\.\\d+)`)\n- `uuid4` (equivalent of `[0-9A-Fa-f]{8}\\-[0-9A-Fa-f]{4}\\-[0-9A-Fa-f]{4}\\-[0-9A-Fa-f]{4}\\-[0-9A-Fa-f]{12}`)\n- `slug` (equivalent of `[a-z0-9]+(?:-[a-z0-9]+)*`)\n- `md5` (equivalent of `[0-9a-fA-F]{32}`)\n- `sha1` (equivalent of `[0-9a-fA-F]{40}`)\n- `domain` (equivalent of `([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}`)\n\n### Router\n\nThe router is the main functionality in the package, it is defined by `Router` class. He is able to find the\ngood `Route` object according to a `ServerRequestInterface` object (see PSR-7).\n\n```php\nuse Berlioz\\Http\\Message\\ServerRequest;\nuse Berlioz\\Router\\Route;\nuse Berlioz\\Router\\Router;\n\n// Create server request or get them from another place in your app\n$serverRequest = new ServerRequest(...);\n\n// Create router\n$router = new Router();\n$router-\u003eaddRoute(\n    new Route('/path-of/my-route'),\n    new Route('/path-of/my-route/{attribute}/with-attribute')\n);\n\n$route = $router-\u003ehandle($serverRequest);\n```\n\n#### Options\n\n| Options            | Type           | Description                                                                               |\n|--------------------|----------------|-------------------------------------------------------------------------------------------|\n| X-Forwarded-Prefix | boolean/string | Default to **false**, **true** to use \"X-Forwarded-Prefix\" value or custom name of header | \n\n#### Generate path\n\nYou can generate a path with some parameters directly with `Router` object.\n\n```php\nuse Berlioz\\Router\\Exception\\NotFoundException;\nuse Berlioz\\Router\\Router;\n\n$router = new Router();\n// ...add routes\n\ntry {\n    $path = $router-\u003egenerate('name-of-route', ['attribute1' =\u003e 'value']);\n} catch (NotFoundException $exception) {\n    // ... not found route\n}\n```\n\nThe return of method, is the path in string format or thrown an exception if not able to generate path (not all required\nparameters for example).\n\n#### Valid path\n\nYou can be valid a `ServerRequestInterface` to known if a path can be treated by a route.\n\n```php\nuse Berlioz\\Http\\Message\\ServerRequest;\nuse Berlioz\\Router\\Router;\n\n$serverRequest = new ServerRequest(...);\n$router = new Router();\n// ...add routes\n\n/** bool $valid Valid path ?*/\n$valid = $router-\u003eisValid($serverRequest);\n```\n\nThe return of method is a `boolean` value.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fberliozframework%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fberliozframework%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fberliozframework%2Frouter/lists"}