1212## Installation
1313
1414``` bash
15- composer require ' sunrise/http-router:^2.13 '
15+ composer require ' sunrise/http-router:^2.14 '
1616```
1717
1818## Support for OpenAPI (Swagger) Specification (optional)
@@ -444,6 +444,28 @@ use Sunrise\Http\Router\Annotation\Route;
444444final class EntryUpdateRequestHandler implements RequestHandlerInterface
445445```
446446
447+ ##### Additional annotations
448+
449+ ``` php
450+ use Sunrise\Http\Router\Annotation\Host;
451+
452+ #[Host('admin')]
453+ #[Prefix('/api/v1')]
454+ #[Postfix('.json')]
455+ #[Middleware(SomeMiddleware::class)]
456+ final class SomeController
457+ {
458+ #[Route('foo', '/foo')]
459+ public function foo(ServerRequestInterface $request) : ResponseInterface
460+ {
461+ // this action will be available at:
462+ // http://admin.host/api/v1/foo.json
463+ //
464+ // this can be handy to reduce code duplication...
465+ }
466+ }
467+ ```
468+
447469---
448470
449471## Useful to know
@@ -469,6 +491,16 @@ $request->getAttribute('@route');
469491$request->getAttribute(\Sunrise\Http\Router\RouteInterface::ATTR_ROUTE);
470492```
471493
494+ #### Through Event
495+
496+ > Available from version 2.13.
497+
498+ ``` php
499+ $eventDispatcher->addListener(RouteEvent::NAME, function (RouteEvent $event) {
500+ $event->getRoute();
501+ });
502+ ```
503+
472504### Generation a route URI
473505
474506``` php
@@ -485,6 +517,8 @@ $response = $router->getRoute('route.name')->handle($request);
485517
486518### Route grouping
487519
520+ Example for annotations [ here] ( #additional-annotations ) .
521+
488522``` php
489523$collector->group(function ($collector) {
490524 $collector->group(function ($collector) {
@@ -528,15 +562,39 @@ $collector->get('api.entry.read', '/api/v1/entry/{slug<@slug>}');
528562$collector->get('api.entry.read', '/api/v1/entry/{id<@id>}');
529563```
530564
565+ It is better to set patterns through the router:
566+
567+ ``` php
568+ // available since version 2.11.0
569+ $router->addPatterns([
570+ '@id' => '[1-9][0-9]*',
571+ ]);
572+ ```
573+
574+ ...or through the router's builder:
575+
576+ ``` php
577+ // available since version 2.11.0
578+ $builder->setPatterns([
579+ '@id' => '[1-9][0-9]*',
580+ ]);
581+ ```
582+
531583### Hosts (available from version 2.6.0)
532584
533585> Note: if you don't assign a host for a route, it will be available on any hosts!
534586
535587``` php
536588// move the hosts table into the settings...
537- $router->addHost('public.host', 'www.example.com');
538- $router->addHost('admin.host', 'secret.example.com');
539- $router->addHost('api.host', 'api.example.com');
589+ $router->addHost('public.host', 'www.example.com', ...);
590+ $router->addHost('admin.host', 'secret.example.com', ...);
591+ $router->addHost('api.host', 'api.example.com', ...);
592+
593+ // ...or:
594+ $router->addHosts([
595+ 'public.host' => ['www.example.com', ...],
596+ ...
597+ ]);
540598
541599// the route will available only on the `secret.example.com` host...
542600$route->setHost('admin.host');
@@ -548,6 +606,28 @@ $collector->group(function ($collector) {
548606->setHost('admin.host');
549607```
550608
609+ You can resolve the hostname since version 2.14.0 as follows:
610+
611+ ``` php
612+ $router->addHost('admin', 'www1.admin.example.com', 'www2.admin.example.com');
613+
614+ $router->resolveHostname('www1.admin.example.com'); // return "admin"
615+ $router->resolveHostname('www2.admin.example.com'); // return "admin"
616+ $router->resolveHostname('unknown'); // return null
617+ ```
618+
619+ Also you can get all routes by hostname:
620+
621+ ``` php
622+ $router->getRoutesByHostname('www1.admin.example.com');
623+ ```
624+
625+ ### Route Holder
626+
627+ ``` php
628+ $route->getHolder(); // return Reflector (class, method or function)
629+ ```
630+
551631### The router builder
552632
553633``` php
@@ -559,6 +639,7 @@ $router = (new RouterBuilder)
559639 ->useDescriptorLoader([]) // array with classes or directory with classes...
560640 ->setHosts([]) //
561641 ->setMiddlewares([]) // array with middlewares...
642+ ->setPatterns([]) // available since version 2.11.0
562643 ->build();
563644```
564645
0 commit comments