Skip to content

Commit 10b7bb9

Browse files
committed
update README.md
1 parent 74361b9 commit 10b7bb9

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
## Installation
1313

1414
```bash
15-
composer require 'sunrise/http-router:^2.6'
15+
composer require 'sunrise/http-router:^2.8'
1616
```
1717

1818
## QuickStart
@@ -81,13 +81,26 @@ $loader->attachArray([
8181
$router = new Router();
8282
$router->load($loader);
8383

84+
// if the router matching should be isolated for top middlewares...
85+
// for example for error handling...
86+
// [!] available from version 2.8
87+
$response = $router->run($request);
88+
8489
// if the router is used as a request handler
8590
$response = $router->handle($request);
8691

8792
// if the router is used as middleware
8893
$response = $router->process($request, $handler);
8994
```
9095

96+
```php
97+
/** @var Sunrise\Http\Router\RouteCollector $this */
98+
99+
$this->get('home', '/', new CallableRequestHandler(function ($request) {
100+
return (new ResponseFactory)->createJsonResponse(200);
101+
}));
102+
```
103+
91104
#### Strategy loading routes from descriptors (annotations or attributes)
92105

93106
```php
@@ -111,6 +124,11 @@ $loader->attachArray([
111124
$router = new Router();
112125
$router->load($loader);
113126

127+
// if the router matching should be isolated for top middlewares...
128+
// for example for error handling...
129+
// [!] available from version 2.8
130+
$response = $router->run($request);
131+
114132
// if the router is used as a request handler
115133
$response = $router->handle($request);
116134

@@ -131,13 +149,56 @@ $collector->get('home', '/', new HomeController());
131149
$router = new Router();
132150
$router->addRoute(...$collector->getCollection()->all());
133151

152+
// if the router matching should be isolated for top middlewares...
153+
// for example for error handling...
154+
// [!] available from version 2.8
155+
$response = $router->run($request);
156+
134157
// if the router is used as a request handler
135158
$response = $router->handle($request);
136159

137160
// if the router is used as middleware
138161
$response = $router->process($request, $handler);
139162
```
140163

164+
#### Error handling example
165+
166+
```php
167+
use Sunrise\Http\Message\ResponseFactory;
168+
use Sunrise\Http\Router\Exception\MethodNotAllowedException;
169+
use Sunrise\Http\Router\Exception\RouteNotFoundException;
170+
use Sunrise\Http\Router\Middleware\CallableMiddleware;
171+
use Sunrise\Http\Router\RequestHandler\CallableRequestHandler;
172+
use Sunrise\Http\Router\RouteCollector;
173+
use Sunrise\Http\Router\Router;
174+
use Sunrise\Http\ServerRequest\ServerRequestFactory;
175+
176+
use function Sunrise\Http\Router\emit;
177+
178+
$collector = new RouteCollector();
179+
180+
$collector->get('home', '/', new CallableRequestHandler(function ($request) {
181+
return (new ResponseFactory)->createJsonResponse(200);
182+
}));
183+
184+
$router = new Router();
185+
$router->addRoute(...$collector->getCollection()->all());
186+
187+
$router->addMiddleware(new CallableMiddleware(function ($request, $handler) {
188+
try {
189+
return $handler->handle($request);
190+
} catch (MethodNotAllowedException $e) {
191+
return (new ResponseFactory)->createResponse(405);
192+
} catch (RouteNotFoundException $e) {
193+
return (new ResponseFactory)->createResponse(404);
194+
} catch (Throwable $e) {
195+
return (new ResponseFactory)->createResponse(500);
196+
}
197+
}));
198+
199+
emit($router->run(ServerRequestFactory::fromGlobals()));
200+
```
201+
141202
#### Route Annotation Example
142203

143204
##### Minimal annotation view

0 commit comments

Comments
 (0)