diff --git a/CHANGELOG.md b/CHANGELOG.md index 66217c2..dd22715 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 0.1.3 Under development - fix: serve debugger pages and build their URLs on the application base URL (`Subfolder` URI prefix or `@baseUrl` alias) for subfolder apps. +- docs: add the middleware and logger integration example and the subfolder setup to `README.md`. ## 0.1.2 October 30, 2026 diff --git a/README.md b/README.md index cd34ca0..e473a2a 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,73 @@ composer yii-config-rebuild ``` Installation publishes configuration, but the application still owns its aggregate configuration groups, logger, and -middleware list. Complete the [Getting started guide](https://github.com/yii3/debug/blob/main/docs/index.md) to connect -all of them. Installing the package or seeing it in the merge plan alone does not prove that the debugger can resolve -from the container or capture a request. +middleware list. In an application built from the `yiisoft/app` template, two definitions connect the debugger. Spread +the merged middleware list before the application middleware in `config/web/di/application.php`: + +```php +use Yiisoft\ErrorHandler\Middleware\ErrorCatcher; +use Yiisoft\Router\Middleware\Router; + +'withMiddlewares()' => [[ + // DebugRouteMiddleware serves the debugger pages under /debug and answers 403 outside allowedIPs. + // RequestCaptureMiddleware captures every other request and injects the toolbar into HTML responses. + ...($params['yiisoft/middleware-dispatcher']['middlewares'] ?? []), + ErrorCatcher::class, + Router::class, +]], +``` + +Build the logger from the merged targets in `config/common/di/logger.php`: + +```php +use Psr\Log\LoggerInterface; +use Yiisoft\Definitions\ReferencesArray; +use Yiisoft\Log\{Logger, StreamTarget}; + +LoggerInterface::class => [ + 'class' => Logger::class, + '__construct()' => [ + // The package merges its `debug` target into this list. A hardcoded list leaves the Logs panel empty. + 'targets' => ReferencesArray::from( + array_values($params['yiisoft/log']['targets'] ?? [StreamTarget::class]), + ), + ], +], +``` + +Every runner must also load the package groups, including `di-web` and `events-web`. The +[Getting started guide](https://github.com/yii3/debug/blob/main/docs/index.md) covers the groups and the checks that +prove a working integration. Installing the package or seeing it in the merge plan alone does not prove that the +debugger can resolve from the container or capture a request. + +### Serve the application under a subfolder + +Under a URL subfolder such as `/api`, the debugger follows the application base URL. Keep `routePrefix` at `/debug` and +open `/api/debug`. Tell the debugger the base in one of two ways: + +```php +use Yiisoft\ErrorHandler\Middleware\ErrorCatcher; +use Yiisoft\Router\Middleware\Router; +use Yiisoft\Yii\Middleware\Subfolder; + +// Option 1, config/web/di/application.php: an auto-detecting Subfolder BEFORE the debugger middleware list. +'withMiddlewares()' => [[ + Subfolder::class, + ...($params['yiisoft/middleware-dispatcher']['middlewares'] ?? []), + ErrorCatcher::class, + Router::class, +]], + +// Option 2, config/common/aliases.php: no Subfolder, for example with every route declared under the prefix. +'@baseUrl' => '/api', +``` + +> [!IMPORTANT] +> +> A `Subfolder` with an explicit `prefix` does not set `@baseUrl`, and one placed after the debugger middleware list +> reaches the debugger too late. Set `'@baseUrl' => '/api'` as well in both cases; otherwise the debugger pages are not +> served under the subfolder or request their assets from the host root. See +> [Serving the application under a subfolder](https://github.com/yii3/debug/blob/main/docs/configuration.md#serving-the-application-under-a-subfolder). ### Basic usage