@@ -18,6 +18,7 @@ composer require wondernetwork/slim-kernel
18
18
``` php
19
19
use WonderNetwork\SlimKernel\KernelBuilder;
20
20
use WonderNetwork\SlimKernel\ServiceFactory\SymfonyConsoleServiceFactory;
21
+ use WonderNetwork\SlimKernel\StartupHook\RoutesStartupHook;
21
22
22
23
// configure the container:
23
24
$container = KernelBuilder::start(
@@ -43,6 +44,10 @@ $container = KernelBuilder::start(
43
44
__DIR__.'/../src/Cli/**/*Command.php',
44
45
),
45
46
)
47
+ ->onStartup(
48
+ // automatically add routes
49
+ new RoutesStartupHook(__DIR__.'/../app/routes/*.php'),
50
+ )
46
51
// only pass a path for prod environments, null otherwise
47
52
->useCache(__DIR__.'/../.cache')
48
53
->build();
@@ -89,6 +94,33 @@ of a CLI command. This is best place to setup some global error handlers, boot s
89
94
static properties, etc. To do so, pass an object implementing ` StartupHook ` to the
90
95
` onStartup() ` method
91
96
97
+ ### Routes Startup Hook
98
+
99
+ ` WonderNetwork\SlimKernel\StartupHook\RouteStartupHook `
100
+
101
+ If you would like to get your routes registered in a declarative manner,
102
+ use this startup hook to point to files containing your route definitions.
103
+ Each of the files matched by the glob pattern ** needs to return** a closure,
104
+ which takes ` Slim\App ` , or more precisely ` Slim\Interfaces\RouteCollectorProxyInterface ` .
105
+ This means you can use it for more than routes: for example global middlewares.
106
+ The closures will be evaluated on a Slim Application fetched from the container.
107
+
108
+ ``` php
109
+ $kernelBuilder->onStartup(
110
+ new \WonderNetwork\SlimKernel\StartupHook\RoutesStartupHook(
111
+ __DIR__.'/../routes/*.php',
112
+ ),
113
+ );
114
+ // app/routes/some.php
115
+ use Psr\Http\Message\ResponseInterface;
116
+ return static function (Slim\App $app) {
117
+ $app->get('/hello/{message}', function (ResponseInterface $response, string $message) {
118
+ return $response->withHeader("X-Hello", $message);
119
+ });
120
+ }
121
+ ```
122
+
123
+
92
124
## Error handling
93
125
94
126
The default error handling middleware is added. It’s configured to silently log
@@ -147,29 +179,3 @@ return new WonderNetwork\SlimKernel\ServiceFactory\SymfonyConsoleServiceFactory(
147
179
'acme v1.0',
148
180
);
149
181
```
150
-
151
- ### Routes Service Factory
152
-
153
- ` WonderNetwork\SlimKernel\ServiceFactory\RouteServiceFactory `
154
-
155
- If you would like to get your routes registered in a declarative manner,
156
- use this service factory to point to files containing your route definitions.
157
- Each of the files matched by the glob pattern ** needs to return** a closure,
158
- which takes ` Slim\App ` , or more precisely ` Slim\Interfaces\RouteCollectorProxyInterface ` .
159
- This means you can use it for more than routes: for example global middlewares.
160
- The closures will be evaluated whenever the Slim Application is fetched from
161
- the container.
162
-
163
- ``` php
164
- // app/services/routes.php
165
- return new WonderNetwork\SlimKernel\ServiceFactory\RoutesServiceFactory(
166
- __DIR__.'/../routes/*.php',
167
- );
168
- // app/routes/some.php
169
- use Psr\Http\Message\ResponseInterface;
170
- return static function (Slim\App $app) {
171
- $app->get('/hello/{message}', function (ResponseInterface $response, string $message) {
172
- return $response->withHeader("X-Hello", $message);
173
- });
174
- }
175
- ```
0 commit comments