@@ -194,25 +194,60 @@ the [`ErrorHandler`](middleware.md#errorhandler) to the list of middleware used.
194194You may also explicitly pass an [ ` ErrorHandler ` ] ( middleware.md#errorhandler )
195195middleware to the ` App ` like this:
196196
197+ === "Using middleware instances"
198+
199+ ```php title="public/index.php"
200+ <?php
201+
202+ require __DIR__ . '/../vendor/autoload.php';
203+
204+ $app = new FrameworkX\App(
205+ new FrameworkX\ErrorHandler()
206+ );
207+
208+ // Register routes here, see routing…
209+
210+ $app->run();
211+ ```
212+
213+ === "Using middleware names"
214+
215+ ```php title="public/index.php"
216+ <?php
217+
218+ require __DIR__ . '/../vendor/autoload.php';
219+
220+ $app = new FrameworkX\App(
221+ FrameworkX\ErrorHandler::class
222+ );
223+
224+ // Register routes here, see routing…
225+
226+ $app->run();
227+ ```
228+
229+ If you do not explicitly pass an [ ` ErrorHandler ` ] ( middleware.md#errorhandler ) or
230+ if you pass another middleware before an [ ` ErrorHandler ` ] ( middleware.md#errorhandler )
231+ to the ` App ` , a default error handler will be added as a first handler automatically.
232+ You may use the [ DI container configuration] ( ../best-practices/controllers.md#container-configuration )
233+ to configure the default error handler like this:
234+
197235``` php title="public/index.php"
198236<?php
199237
200238require __DIR__ . '/../vendor/autoload.php';
201239
202- $app = new FrameworkX\App(
203- new FrameworkX\ErrorHandler()
204- );
240+ $container = new FrameworkX\Container([
241+ FrameworkX\ErrorHandler::class => fn () => new FrameworkX\ErrorHandler()
242+ ]);
243+
244+ $app = new FrameworkX\App($container);
205245
206246// Register routes here, see routing…
207247
208248$app->run();
209249```
210250
211- > ⚠️ ** Feature preview**
212- >
213- > Note that the [ ` ErrorHandler ` ] ( middleware.md#errorhandler ) may currently only
214- > be passed as a middleware instance and not as a middleware name to the ` App ` .
215-
216251By default, this error message contains only few details to the client to avoid
217252leaking too much internal information.
218253If you want to implement custom error handling, you're recommended to either
@@ -245,28 +280,68 @@ adding the [`AccessLogHandler`](middleware.md#accessloghandler) to the list of
245280middleware used. You may also explicitly pass an [ ` AccessLogHandler ` ] ( middleware.md#accessloghandler )
246281middleware to the ` App ` like this:
247282
248- ``` php title="public/index.php"
249- <?php
283+ === "Using middleware instances"
250284
251- require __DIR__ . '/../vendor/autoload.php';
285+ ```php title="public/index.php"
286+ <?php
252287
253- $app = new FrameworkX\App(
254- new FrameworkX\AccessLogHandler(),
255- new FrameworkX\ErrorHandler()
256- );
288+ require __DIR__ . '/../vendor/autoload.php';
257289
258- // Register routes here, see routing…
290+ $app = new FrameworkX\App(
291+ new FrameworkX\AccessLogHandler(),
292+ new FrameworkX\ErrorHandler()
293+ );
259294
260- $app->run();
261- ```
295+ // Register routes here, see routing…
296+
297+ $app->run();
298+ ```
299+
300+ === "Using middleware names"
301+
302+ ```php title="public/index.php"
303+ <?php
304+
305+ require __DIR__ . '/../vendor/autoload.php';
306+
307+ $app = new FrameworkX\App(
308+ FrameworkX\AccessLogHandler::class,
309+ FrameworkX\ErrorHandler::class
310+ );
311+
312+ // Register routes here, see routing…
313+
314+ $app->run();
315+ ```
262316
263317> ⚠️ ** Feature preview**
264318>
265319> Note that the [ ` AccessLogHandler ` ] ( middleware.md#accessloghandler ) may
266- > currently only be passed as a global middleware instance and not as a global
267- > middleware name to the ` App ` and may not be used for individual routes.
320+ > currently only be passed as a global middleware to the ` App ` and may not be
321+ > used for individual routes.
268322
269323If you pass an [ ` AccessLogHandler ` ] ( middleware.md#accessloghandler ) to the ` App ` ,
270324it must be followed by an [ ` ErrorHandler ` ] ( middleware.md#errorhandler ) like in
271325the previous example. See also [ error handling] ( #error-handling ) for more
272326details.
327+
328+ If you do not explicitly pass an [ ` AccessLogHandler ` ] ( middleware.md#accessloghandler )
329+ to the ` App ` , a default access log handler will be added as a first handler automatically.
330+ You may use the [ DI container configuration] ( ../best-practices/controllers.md#container-configuration )
331+ to configure the default access log handler like this:
332+
333+ ``` php title="public/index.php"
334+ <?php
335+
336+ require __DIR__ . '/../vendor/autoload.php';
337+
338+ $container = new FrameworkX\Container([
339+ FrameworkX\AccessLogHandler::class => fn () => new FrameworkX\AccessLogHandler()
340+ ]);
341+
342+ $app = new FrameworkX\App($container);
343+
344+ // Register routes here, see routing…
345+
346+ $app->run();
347+ ```
0 commit comments