{"id":20719744,"url":"https://github.com/slince/routing","last_synced_at":"2025-04-23T14:25:47.672Z","repository":{"id":57053329,"uuid":"48089581","full_name":"slince/routing","owner":"slince","description":":dolphin: A flexible web routing component.","archived":false,"fork":false,"pushed_at":"2017-08-16T02:05:54.000Z","size":89,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T00:31:38.581Z","etag":null,"topics":["matching","psr-7","route","router","routing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/slince.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-16T05:28:16.000Z","updated_at":"2019-08-05T13:12:50.000Z","dependencies_parsed_at":"2022-08-24T05:20:23.040Z","dependency_job_id":null,"html_url":"https://github.com/slince/routing","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Frouting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Frouting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Frouting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/slince%2Frouting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/slince","download_url":"https://codeload.github.com/slince/routing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250449986,"owners_count":21432580,"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":["matching","psr-7","route","router","routing"],"created_at":"2024-11-17T03:18:14.042Z","updated_at":"2025-04-23T14:25:47.653Z","avatar_url":"https://github.com/slince.png","language":"PHP","readme":"﻿# Routing Component\n\n[![Build Status](https://img.shields.io/travis/slince/routing/master.svg?style=flat-square)](https://travis-ci.org/slince/routing)\n[![Coverage Status](https://img.shields.io/codecov/c/github/slince/routing.svg?style=flat-square)](https://codecov.io/github/slince/routing)\n[![Latest Stable Version](https://img.shields.io/packagist/v/slince/routing.svg?style=flat-square\u0026label=stable)](https://packagist.org/packages/slince/routing)\n[![Scrutinizer](https://img.shields.io/scrutinizer/g/slince/routing.svg?style=flat-square)](https://scrutinizer-ci.com/g/slince/routing/?branch=master)\n\nA flexible web routing component.\n\n## Installation\n\nInstall via composer\n\n```bash\ncomposer require slince/routing\n```\n\n## Quick example\n\n```php\n$routes = new Slince\\Routing\\RouteCollection();\n$routes-\u003eget('/products', 'Products::index')-\u003esetName('product_index');\n\n$request = Zend\\Diactoros\\ServerRequestFactory::fromGlobals(); //Creates the psr7 request instance\n\n$matcher = new Slince\\Routing\\Matcher($routes);\n$generator = new Slince\\Routing\\Generator($request);\n\n$route = $matcher-\u003ematchRequest($request); //Matches the current request\nvar_dump($route-\u003egetComputedParamters()); //Dumps route computed paramters\necho $generator-\u003egenerate($route); //Generates path \n\n$route = $routes-\u003egetByAction('Products::index');\necho $generator-\u003egenerate($route); //Generates path \n\n$route = $routes-\u003egetByName('product_index');\necho $generator-\u003egenerate($route); //Generates path \n```\n\n## Usage\n\n### Defines routes\n\n#### Creates an instance of `Slince\\Routing\\RouteCollection` first,\n\n```php\n$routes = new Slince\\Routing\\RouteCollection();\n$route = new Slince\\Routing\\Route('/products/{id}', 'Products::view');\n$routes-\u003eadd($route);\n```\nThe route path contain the placeholder `{id}` which matches everything except \"/\" and \".\"\nYou can set custom requirements with `setRequirement`  method or `setRequirements` method.\n\n```php\n$route-\u003esetRequirements([\n    'id' =\u003e '\\d+'\n]);\n```\n\nRouting supports optional placeholder, you can provide a default value for the placeholder.\n\n```php\n$route-\u003esetDefaults([\n    'id' =\u003e 1\n]);\n```\nThe route can match `/products` and `/products/1`.\n\n#### Shorthands for HTTP methods are also provided.\n\n```php\n$routes = new RouteCollection();\n\n$routes-\u003eget('/pattern', 'action');\n$routes-\u003epost('/pattern', 'action');\n$routes-\u003eput('/pattern', 'action');\n$routes-\u003edelete('/pattern', 'action');\n$routes-\u003eoptions('/pattern', 'action');\n$routes-\u003epatch('/pattern', 'action');\n```\n\n### Customize HTTP verb \n\n```php\n$route-\u003esetMethods(['GET', 'POST', 'PUT']);\n```\n\n### Host matching\n\nYou can limit a route to specified host with `setHost` method.\n\n```php\n$routes-\u003ecreate('/products', 'Products::index')\n    -\u003esetHost('product.domain.com');\n```\n\nThe route will only match the request with `product.domain.com` domain\n\n### Force route use HTTPS or HTTP\n\nRouting also allow you to define routes using `http` and `https`.\n\n```php\n$routes = new Slince\\Routing\\RouteCollection();\n\n$routes-\u003ehttps('/pattern', 'action');\n$routes-\u003ehttp('/pattern', 'action');\n```\nOr customize this.\n\n```php\n$route-\u003esetSchemes(['http', 'https']);\n```\n\n### Match a path or psr7 request.\n\n```php\n$routes = new Slince\\Routing\\RouteCollection();\n$routes-\u003ecreate('/products/{id}.{_format}', 'Products::view');\n$matcher = new Slince\\Routing\\Matcher($routes);\n\ntry {\n    $route = $matcher-\u003ematch('/products/10.html');\n    \n    print_r($route-\u003egetComputedParameters())// ['id' =\u003e 10, '_format' =\u003e 'html']\n    \n} catch (Slince\\Routing\\Exception\\RouteNotFoundException $e) {\n    //404\n}\n```\nMatcher will return the matching route. If no matching route can be found, matcher will throw a `RouteNotFoundException`.\n\nMatch a ` Psr\\Http\\Message\\ServerRequestInterface`.\n\n```php\n$request = Zend\\Diactoros\\ServerRequestFactory::fromGlobals();\ntry {\n    $route = $matcher-\u003ematchRequest($request);\n} catch (Slince\\Routing\\Exception\\MethodNotAllowedException $e) {\n    //403\n    var_dump($e-\u003egetAllowedMethods());\n} catch (Slince\\Routing\\Exception\\RouteNotFoundException $e) {\n    //404\n}\n```\n\n### Generate path for a route\n\n```php\n$generator = new Slince\\Routing\\Generator();\n\n$route = new Slince\\Routing\\Route('/foo/{id}', 'action');\necho $generator-\u003egenerate($route, ['id' =\u003e 10]); //will output \"/foo/10\"\n```\n\nIf you want generate the absolute url for the route, you need to provide generator with a request as request context.\n\n```php\n$request = Zend\\Diactoros\\ServerRequestFactory::fromGlobals();\n$generator-\u003esetRequest($request);\n\necho $generator-\u003egenerate($route, ['id' =\u003e 10], true); //will output \"{scheme}://{domain}/foo/10\"\n```\n\n## License\n \nThe MIT license. See [MIT](https://opensource.org/licenses/MIT)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslince%2Frouting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslince%2Frouting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslince%2Frouting/lists"}