|
1 | 1 | # HTTP Router |
2 | 2 |
|
3 | | -The repository is under development, and the description will appear later. |
| 3 | +[](https://packagist.org/packages/httpsoft/http-router) |
| 4 | +[](https://packagist.org/packages/httpsoft/http-router) |
| 5 | +[](https://packagist.org/packages/httpsoft/http-router) |
| 6 | +[](https://github.com/httpsoft/http-router/actions) |
| 7 | +[](https://github.com/httpsoft/http-router/actions) |
| 8 | +[](https://scrutinizer-ci.com/g/httpsoft/http-router/?branch=master) |
| 9 | +[](https://scrutinizer-ci.com/g/httpsoft/http-router/?branch=master) |
| 10 | + |
| 11 | +This package provides convenient management of HTTP request routing with support for [PSR-7](https://github.com/php-fig/http-message) and [PSR-15](https://github.com/php-fig/http-factory). |
| 12 | + |
| 13 | +## Documentation |
| 14 | + |
| 15 | +* [In English language](https://httpsoft.org/docs/router). |
| 16 | +* [In Russian language](https://httpsoft.org/ru/docs/router). |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +This package requires PHP version 7.4 or later. |
| 21 | + |
| 22 | +``` |
| 23 | +composer require httpsoft/http-router |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +```php |
| 29 | +use HttpSoft\Router\RouteCollector; |
| 30 | + |
| 31 | +/** |
| 32 | + * @var mixed $handler |
| 33 | + */ |
| 34 | + |
| 35 | +$router = new RouteCollector(); |
| 36 | + |
| 37 | +// Defining routes. |
| 38 | +$router->get('home', '/', $handler); |
| 39 | +$router->post('logout', '/logout', $handler); |
| 40 | +$router->add('login', '/login', $handler, ['GET', 'POST']); |
| 41 | + |
| 42 | +// Custom regular expressions for placeholder parameter tokens. |
| 43 | +$router->delete('post.delete', '/post/delete/{id}', $handler)->tokens(['id' => '\d+']); |
| 44 | + |
| 45 | +// Generate path '/post/delete/25' |
| 46 | +$router->routes()->path('post.delete', ['id' => 25]); |
| 47 | +// Generate url '//example.com/post/delete/25' |
| 48 | +$router->routes()->url('post.delete', ['id' => 25], 'example.com'); |
| 49 | +// Generate url 'https://example.com/post/delete/25' |
| 50 | +$router->routes()->url('post.delete', ['id' => 25], 'example.com', true); |
| 51 | +``` |
| 52 | + |
| 53 | +Set the parameter to the default value. |
| 54 | + |
| 55 | +```php |
| 56 | +$router->get('post.view', '/post/{slug}{format}', $handler) |
| 57 | + ->tokens(['slug' => '[\w\-]+', 'format' => '\.[a-zA-z]{3,}']) |
| 58 | + ->defaults(['format' => '.html']) |
| 59 | +; |
| 60 | + |
| 61 | +// Generate path '/post/post-slug.html'. |
| 62 | +$router->routes()->path('post.view', ['slug' => 'post-slug']); |
| 63 | +``` |
| 64 | + |
| 65 | +Tokens of the route enclosed in `[...]` are considered optional. |
| 66 | + |
| 67 | +```php |
| 68 | +$router->get('post.list', '/posts{[page]}', $handler) |
| 69 | + ->tokens(['page' => '\d+']) |
| 70 | +; |
| 71 | + |
| 72 | +// '/posts/33' |
| 73 | +$router->routes()->path('post.list', ['page' => 33]); |
| 74 | +// '/posts' |
| 75 | +$router->routes()->path('post.list'); |
| 76 | +``` |
| 77 | + |
| 78 | +If necessary, you can specify a specific host for route matching. |
| 79 | + |
| 80 | +```php |
| 81 | +// Only for example.com |
| 82 | +$router->get('page', '/page', $handler) |
| 83 | + ->host('example.com') |
| 84 | +; |
| 85 | + |
| 86 | +// Only for subdomain.example.com |
| 87 | +$router->get('page', '/page', $handler) |
| 88 | + ->host('subdomain.example.com') |
| 89 | +; |
| 90 | + |
| 91 | +// Only for shop.example.com or blog.example.com |
| 92 | +$router->get('page', '/page', $handler) |
| 93 | + ->host('(shop|blog).example.com') |
| 94 | +; |
| 95 | +``` |
| 96 | + |
| 97 | +You can specify routes inside of a group. |
| 98 | + |
| 99 | +```php |
| 100 | +$router->group('/post', static function (RouteCollector $router): void { |
| 101 | + // '/post/post-slug' |
| 102 | + $router->get('post.view', '/{slug}', ViewHandler::class)->tokens(['slug' => '[\w-]+']); |
| 103 | + // '/post' or '/post/page-14' |
| 104 | + $router->get('post.list', '{[page]}', ListHandler::class)->tokens(['page' => 'page-\d+']); |
| 105 | +}); |
| 106 | + |
| 107 | +// The result will be equivalent to: |
| 108 | + |
| 109 | +$router->get('post.view', '/post/{slug}', ViewHandler::class)->tokens(['slug' => '[\w-]+']); |
| 110 | +$router->get('post.list', '/post{[page]}', ListHandler::class)->tokens(['page' => 'page-\d+']); |
| 111 | +``` |
| 112 | + |
| 113 | +Check matching routes. |
| 114 | + |
| 115 | +```php |
| 116 | +/** |
| 117 | + * @var mixed $handler |
| 118 | + * @var Psr\Http\Message\UriInterface $uri |
| 119 | + * @var Psr\Http\Message\ServerRequestInterface $request |
| 120 | + */ |
| 121 | + |
| 122 | +$router->get('page', '/page/{id}', $handler)->tokens(['id' => '\d+']); |
| 123 | + |
| 124 | +// Match |
| 125 | +$route = $router->routes()->match($request->withUri($uri->withPath('/page/11'))); |
| 126 | +$route->getMatchedParameters(); // ['id' => '11'] |
| 127 | + |
| 128 | +// Mismatch |
| 129 | +$router->routes()->match($request->withUri($uri->withPath('/page/slug'))); // null |
| 130 | +``` |
0 commit comments