@@ -34,6 +34,9 @@ use function Sunrise\Http\Router\emit;
3434
3535$collector = new RouteCollector();
3636
37+ // set container if necessary...
38+ $collector->setContainer($container);
39+
3740$collector->get('home', '/', new CallableRequestHandler(function ($request) {
3841 return (new ResponseFactory)->createJsonResponse(200, [
3942 'status' => 'ok',
@@ -55,13 +58,20 @@ emit($response);
5558
5659Study [ sunrise/awesome-skeleton] ( https://github.com/sunrise-php/awesome-skeleton ) to understand how this can be used.
5760
58- #### Strategy loading routes from configs
61+ #### Strategy for loading routes from configs
62+
63+ > Please note that since version 2.10.0 class ` ConfigLoader ` must be used.
5964
6065``` php
61- use Sunrise\Http\Router\Loader\CollectableFileLoader ;
66+ use Sunrise\Http\Router\Loader\ConfigLoader ;
6267use Sunrise\Http\Router\Router;
6368
64- $loader = new CollectableFileLoader();
69+ $loader = new ConfigLoader();
70+
71+ // set container if necessary...
72+ $loader->setContainer($container);
73+
74+ // attach configs...
6575$loader->attach('routes/api.php');
6676$loader->attach('routes/admin.php');
6777$loader->attach('routes/public.php');
@@ -78,6 +88,9 @@ $loader->attachArray([
7888 'routes/public.php',
7989]);
8090
91+ // install container if necessary...
92+ $loader->setContainer($container);
93+
8194$router = new Router();
8295$router->load($loader);
8396
@@ -99,25 +112,57 @@ $response = $router->process($request, $handler);
99112$this->get('home', '/', new CallableRequestHandler(function ($request) {
100113 return (new ResponseFactory)->createJsonResponse(200);
101114}));
115+
116+ // or using a direct reference to a request handler...
117+ $this->get('home', '/', new App\Http\Controller\HomeController());
118+ ```
119+
120+ > Please note that since version 2.10.0 you can refer to the request handler in different ways.
121+
122+ ``` php
123+ /** @var Sunrise\Http\Router\RouteCollector $this */
124+
125+ $this->get('home', '/', function ($request) {
126+ return (new ResponseFactory)->createJsonResponse(200);
127+ });
128+
129+ $this->get('home', '/', App\Http\Controller\HomeController::class, [
130+ App\Http\Middleware\FooMiddleware::class,
131+ App\Http\Middleware\BarMiddleware::class,
132+ ]);
133+
134+ $this->get('home', '/', [App\Http\Controller\HomeController::class, 'index'], [
135+ App\Http\Middleware\FooMiddleware::class,
136+ App\Http\Middleware\BarMiddleware::class,
137+ ]);
102138```
103139
104- #### Strategy loading routes from descriptors (annotations or attributes)
140+ #### Strategy for loading routes from descriptors (annotations or attributes)
105141
106142Install the [ doctrine/annotations] ( https://github.com/doctrine/annotations ) package if you will be use annotations:
107143
108144``` bash
109145composer require doctrine/annotations
110146```
111147
148+ > Please note that since version 2.10.0 class ` DescriptorLoader ` must be used.
149+
150+ > Please note that since version 2.10.0 you can bind the @Rote () annotation to a class methods.
151+
112152``` php
113153use Doctrine\Common\Annotations\AnnotationRegistry;
114- use Sunrise\Http\Router\Loader\DescriptorDirectoryLoader ;
154+ use Sunrise\Http\Router\Loader\DescriptorLoader ;
115155use Sunrise\Http\Router\Router;
116156
117157// necessary if you will use annotations (annotations isn't attributes)...
118158AnnotationRegistry::registerLoader('class_exists');
119159
120- $loader = new DescriptorDirectoryLoader();
160+ $loader = new DescriptorLoader();
161+
162+ // set container if necessary...
163+ $loader->setContainer($container);
164+
165+ // attach a directory with controllers...
121166$loader->attach('src/Controller');
122167
123168// or attach an array
@@ -127,6 +172,10 @@ $loader->attachArray([
127172 'src/Bundle/BundleName/Controller',
128173]);
129174
175+ // or attach a class only
176+ // [!] available from 2.10 version.
177+ $loader->attach(App\Http\Controller\FooController::class);
178+
130179$router = new Router();
131180$router->load($loader);
132181
@@ -150,6 +199,10 @@ use Sunrise\Http\Router\RouteCollector;
150199use Sunrise\Http\Router\Router;
151200
152201$collector = new RouteCollector();
202+
203+ // set container if necessary...
204+ $collector->setContainer($container);
205+
153206$collector->get('home', '/', new HomeController());
154207
155208$router = new Router();
@@ -227,7 +280,7 @@ $route = $collector->get('home', '/', HomeController::class, [
227280##### Config loader
228281
229282``` php
230- $loader = new CollectableFileLoader ();
283+ $loader = new ConfigLoader ();
231284
232285/** @var \Psr\Container\ContainerInterface $container */
233286
@@ -241,7 +294,7 @@ $routes = $loader->load();
241294##### Descriptor loader
242295
243296``` php
244- $loader = new DescriptorDirectoryLoader ();
297+ $loader = new DescriptorLoader ();
245298
246299/** @var \Psr\Container\ContainerInterface $container */
247300
@@ -255,7 +308,7 @@ $routes = $loader->load();
255308#### Descriptors cache (PSR-16)
256309
257310``` php
258- $loader = new DescriptorDirectoryLoader ();
311+ $loader = new DescriptorLoader ();
259312
260313/** @var \Psr\SimpleCache\CacheInterface $cache */
261314
@@ -320,7 +373,7 @@ final class EntryUpdateRequestHandler implements RequestHandlerInterface
320373##### Minimal attribute view
321374
322375``` php
323- use Sunrise\Http\Router\Attribute \Route;
376+ use Sunrise\Http\Router\Annotation \Route;
324377
325378#[Route(
326379 name: 'api_v1_entry_update',
@@ -333,7 +386,7 @@ final class EntryUpdateRequestHandler implements RequestHandlerInterface
333386##### Full attribute
334387
335388``` php
336- use Sunrise\Http\Router\Attribute \Route;
389+ use Sunrise\Http\Router\Annotation \Route;
337390
338391#[Route(
339392 name: 'api_v1_entry_update',
@@ -426,10 +479,14 @@ $collector->group(function ($collector) {
426479 })
427480 ->addPrefix('/entry') // add the prefix to the group...
428481 ->prependMiddleware(...); // add the middleware(s) to the group...
429- })
482+ }, [
483+ App\Http\Middleware\Bar::class, // resolvable middlewares...
484+ ])
430485 ->addPrefix('/v1') // add the prefix to the group...
431486 ->prependMiddleware(...); // add the middleware(s) to the group...
432- })
487+ }, [
488+ App\Http\Middleware\Foo::class, // resolvable middlewares...
489+ ])
433490->addPrefix('/api') // add the prefix to the group...
434491->prependMiddleware(...); // add the middleware(s) to the group...
435492```
@@ -476,14 +533,26 @@ $collector->group(function ($collector) {
476533->setHost('admin.host');
477534```
478535
536+ ### The router builder
537+
538+ ``` php
539+ $router = (new RouterBuilder)
540+ ->setContainer(null) // null or PSR-11 container instance...
541+ ->setCache(null) // null or PSR-16 cache instance... (only for descriptor loader)
542+ ->setCacheKey(null) // null or string... (only for descriptor loader)
543+ ->useConfigLoader([]) // array with files or directory with files...
544+ ->useDescriptorLoader([]) // array with classes or directory with classes...
545+ ->setHosts([]) //
546+ ->setMiddlewares([]) // array with middlewares...
547+ ->build();
548+ ```
549+
479550### CLI commands
480551
481552``` php
482553use Sunrise\Http\Router\Command\RouteListCommand;
483- use Sunrise\Http\Router\Command\RouteMatchCommand;
484554
485555new RouteListCommand($router);
486- new RouteMatchCommand($router);
487556```
488557
489558---
0 commit comments