Skip to content

Commit 164e8dc

Browse files
committed
Add description to README.md
1 parent f4b75d7 commit 164e8dc

File tree

1 file changed

+128
-1
lines changed

1 file changed

+128
-1
lines changed

README.md

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,130 @@
11
# HTTP Router
22

3-
The repository is under development, and the description will appear later.
3+
[![License](https://poser.pugx.org/httpsoft/http-router/license)](https://packagist.org/packages/httpsoft/http-router)
4+
[![Latest Stable Version](https://poser.pugx.org/httpsoft/http-router/v)](https://packagist.org/packages/httpsoft/http-router)
5+
[![Total Downloads](https://poser.pugx.org/httpsoft/http-router/downloads)](https://packagist.org/packages/httpsoft/http-router)
6+
[![GitHub Build Status](https://github.com/httpsoft/http-router/workflows/build/badge.svg)](https://github.com/httpsoft/http-router/actions)
7+
[![GitHub Static Analysis Status](https://github.com/httpsoft/http-router/workflows/static/badge.svg)](https://github.com/httpsoft/http-router/actions)
8+
[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/httpsoft/http-router/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/httpsoft/http-router/?branch=master)
9+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/httpsoft/http-router/badges/quality-score.png?b=master)](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

Comments
 (0)