1- # HTTP router for PHP 7.1+ (incl. PHP 8) 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)
22
3- [ ![ Build Status] ( https://scrutinizer-ci .com/g /sunrise-php/http-router/badges/build.png?b=master )] ( https://scrutinizer-ci .com/g /sunrise-php/http-router/build-status/master )
3+ [ ![ 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 )
55[ ![ Scrutinizer Code Quality] ( https://scrutinizer-ci.com/g/sunrise-php/http-router/badges/quality-score.png?b=master )] ( https://scrutinizer-ci.com/g/sunrise-php/http-router/?branch=master )
66[ ![ Total Downloads] ( https://poser.pugx.org/sunrise/http-router/downloads?format=flat )] ( https://packagist.org/packages/sunrise/http-router )
1212## Installation
1313
1414``` bash
15- composer require ' sunrise/http-router:^2.5 '
15+ composer require ' sunrise/http-router:^2.6 '
1616```
1717
1818## QuickStart
1919
20- The example uses other sunrise packages, but you can use for example ` zend/diactoros ` , or any other.
20+ The example uses other sunrise packages, but you can use, for example, ` zend/diactoros ` or any other.
2121
2222``` bash
2323composer require sunrise/http-message sunrise/http-server-request
@@ -88,16 +88,17 @@ $response = $router->handle($request);
8888$response = $router->process($request, $handler);
8989```
9090
91- #### Strategy loading routes from annotations
91+ #### Strategy loading routes from descriptors ( annotations or attributes)
9292
9393``` php
9494use Doctrine\Common\Annotations\AnnotationRegistry;
95- use Sunrise\Http\Router\Loader\AnnotationDirectoryLoader ;
95+ use Sunrise\Http\Router\Loader\DescriptorDirectoryLoader ;
9696use Sunrise\Http\Router\Router;
9797
98+ // necessary if you will use annotations (annotations isn't attributes)...
9899AnnotationRegistry::registerLoader('class_exists');
99100
100- $loader = new AnnotationDirectoryLoader ();
101+ $loader = new DescriptorDirectoryLoader ();
101102$loader->attach('src/Controller');
102103
103104// or attach an array
@@ -139,10 +140,26 @@ $response = $router->process($request, $handler);
139140
140141#### Route Annotation Example
141142
143+ ##### Minimal annotation view
144+
142145``` php
143146/**
144147 * @Route(
145- * name="apiEntryUpdate",
148+ * name="api_v1_entry_update",
149+ * path="/api/v1/entry/{id<@uuid>}(/{optionalAttribute})",
150+ * methods={"PATCH"},
151+ * )
152+ */
153+ final class EntryUpdateRequestHandler implements RequestHandlerInterface
154+ ```
155+
156+ ##### Full annotation
157+
158+ ``` php
159+ /**
160+ * @Route(
161+ * name="api_v1_entry_update",
162+ * host="api.host",
146163 * path="/api/v1/entry/{id<@uuid>}(/{optionalAttribute})",
147164 * methods={"PATCH"},
148165 * middlewares={
@@ -158,6 +175,47 @@ $response = $router->process($request, $handler);
158175 * priority=0,
159176 * )
160177 */
178+ final class EntryUpdateRequestHandler implements RequestHandlerInterface
179+ ```
180+
181+ #### Route Attribute Example
182+
183+ ##### Minimal attribute view
184+
185+ ``` php
186+ use Sunrise\Http\Router\Attribute\Route;
187+
188+ #[Route(
189+ name: 'api_v1_entry_update',
190+ path: '/api/v1/entry/{id<@uuid>}(/{optionalAttribute})',
191+ methods: ['PATCH'],
192+ )]
193+ final class EntryUpdateRequestHandler implements RequestHandlerInterface
194+ ```
195+
196+ ##### Full attribute
197+
198+ ``` php
199+ use Sunrise\Http\Router\Attribute\Route;
200+
201+ #[Route(
202+ name: 'api_v1_entry_update',
203+ host: 'api.host',
204+ path: '/api/v1/entry/{id<@uuid>}(/{optionalAttribute})',
205+ methods: ['PATCH'],
206+ middlewares: [
207+ \App\Middleware\CorsMiddleware::class,
208+ \App\Middleware\ApiAuthMiddleware::class,
209+ ],
210+ attributes: [
211+ 'optionalAttribute' => 'defaultValue',
212+ ],
213+ summary: 'Updates an entry by UUID',
214+ description: 'Here you can describe the method in more detail...',
215+ tags: ['api', 'entry'],
216+ priority: 0,
217+ )]
218+ final class EntryUpdateRequestHandler implements RequestHandlerInterface
161219```
162220
163221---
@@ -245,6 +303,26 @@ $collector->group(function ($collector) {
245303$collector->get('api.entry.read', '/api/v1/entry/{id<\d+>}(/{optional<\w+>})');
246304```
247305
306+ ### Hosts (available from version 2.6.0)
307+
308+ > Note: if you don't assign a host for a route, it will be available on any hosts!
309+
310+ ``` php
311+ // move the hosts table into the settings...
312+ $router->addHost('public.host', 'www.example.com');
313+ $router->addHost('admin.host', 'secret.example.com');
314+ $router->addHost('api.host', 'api.example.com');
315+
316+ // the route will available only on the `secret.example.com` host...
317+ $route->setHost('admin.host');
318+
319+ // routes in the group will available on the `secret.example.com` host...
320+ $collector->group(function ($collector) {
321+ // some code...
322+ })
323+ ->setHost('admin.host');
324+ ```
325+
248326---
249327
250328## Test run
0 commit comments