Skip to content

Commit 38f393f

Browse files
committed
update README.md
1 parent c49a324 commit 38f393f

File tree

1 file changed

+27
-52
lines changed

1 file changed

+27
-52
lines changed

README.md

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)
1+
# HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenAPI (Swagger) Specification
22

33
[![Build Status](https://circleci.com/gh/sunrise-php/http-router.svg?style=shield)](https://circleci.com/gh/sunrise-php/http-router)
44
[![Code Coverage](https://scrutinizer-ci.com/g/sunrise-php/http-router/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-router/?branch=master)
@@ -15,17 +15,24 @@
1515
composer require 'sunrise/http-router:^2.10'
1616
```
1717

18+
## Support for OpenAPI (Swagger) Specification (optional)
19+
20+
```bash
21+
composer require 'sunrise/http-router-openapi:^2.0'
22+
```
23+
24+
More details can be found here: [sunrise/http-router-openapi](https://github.com/sunrise-php/http-router-openapi).
25+
1826
## QuickStart
1927

20-
The example uses other sunrise packages, but you can use, for example, `zend/diactoros` or any other.
28+
This example uses other sunrise packages, but you can use e.g. `zend/diactoros` or any other.
2129

2230
```bash
2331
composer require sunrise/http-message sunrise/http-server-request
2432
```
2533

2634
```php
2735
use Sunrise\Http\Message\ResponseFactory;
28-
use Sunrise\Http\Router\RequestHandler\CallableRequestHandler;
2936
use Sunrise\Http\Router\RouteCollector;
3037
use Sunrise\Http\Router\Router;
3138
use Sunrise\Http\ServerRequest\ServerRequestFactory;
@@ -34,14 +41,24 @@ use function Sunrise\Http\Router\emit;
3441

3542
$collector = new RouteCollector();
3643

37-
// set container if necessary...
38-
$collector->setContainer($container);
44+
// PSR-15 request handler (optimal performance):
45+
$collector->get('home', '/', new HomeRequestHandler());
3946

40-
$collector->get('home', '/', new CallableRequestHandler(function ($request) {
41-
return (new ResponseFactory)->createJsonResponse(200, [
42-
'status' => 'ok',
43-
]);
44-
}));
47+
// or you can use an anonymous function as your request handler:
48+
$collector->get('home', '/', function ($request) {
49+
return (new ResponseFactory)->createResponse(200);
50+
});
51+
52+
// or you can use the name of a class that implements PSR-15:
53+
$collector->get('home', '/', HomeRequestHandler::class);
54+
55+
// or you can use a class method name as your request handler:
56+
// (note that such a class mayn't implement PSR-15)
57+
$collector->get('home', '/', [HomeRequestHandler::class, 'index']);
58+
59+
// most likely you will need to use PSR-11 container:
60+
// (note that only named classes will be pulled from such a container)
61+
$collector->setContainer($container);
4562

4663
$router = new Router();
4764
$router->addRoute(...$collector->getCollection()->all());
@@ -412,48 +429,6 @@ final class EntryUpdateRequestHandler implements RequestHandlerInterface
412429

413430
## Useful to know
414431

415-
### OpenApi (Swagger)
416-
417-
```bash
418-
composer require 'sunrise/http-router-openapi:^1.1'
419-
```
420-
421-
#### Generation documentation for Swagger (OAS)
422-
423-
```php
424-
use Sunrise\Http\Router\OpenApi\Object\Info;
425-
use Sunrise\Http\Router\OpenApi\OpenApi;
426-
427-
$openApi = new OpenApi(new Info('0.0.1', 'API'));
428-
429-
$openApi->addRoute(...$router->getRoutes());
430-
431-
$openApi->toArray();
432-
```
433-
434-
#### Validation a request body via Swagger documentation
435-
436-
```php
437-
use Sunrise\Http\Router\OpenApi\Middleware\RequestBodyValidationMiddleware;
438-
439-
$route->addMiddleware(new RequestBodyValidationMiddleware());
440-
```
441-
442-
or using annotations:
443-
444-
```php
445-
/**
446-
* @Route(
447-
* name="foo",
448-
* path="/foo",
449-
* methods={"post"},
450-
* middlewares={
451-
* "Sunrise\Http\Router\OpenApi\Middleware\RequestBodyValidationMiddleware",
452-
* },
453-
* )
454-
*/
455-
```
456-
457432
### Generation a route URI
458433

459434
```php

0 commit comments

Comments
 (0)