Skip to content

fix(laravel): expose ReDoc/Scalar via UI switcher in docs view#7942

Open
abderrahimghazali wants to merge 1 commit intoapi-platform:mainfrom
abderrahimghazali:fix/laravel-redoc-discoverability
Open

fix(laravel): expose ReDoc/Scalar via UI switcher in docs view#7942
abderrahimghazali wants to merge 1 commit intoapi-platform:mainfrom
abderrahimghazali:fix/laravel-redoc-discoverability

Conversation

@abderrahimghazali
Copy link
Copy Markdown

@abderrahimghazali abderrahimghazali commented May 1, 2026

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Issues Closes #7682
License MIT

Problem

In the Laravel package, the /api/docs page only ever rendered Swagger UI even when redoc.enabled and scalar.enabled were true in config/api-platform.php. The Symfony bundle ships a UI switcher in its Twig template; the Laravel Blade template never surfaced one, so ReDoc and Scalar were effectively undiscoverable through the UI even though the wiring (config flags, processor params, ?ui= query handling) was already in place.

While digging in I also noticed that originalRoute / originalRouteParams in SwaggerUiProcessor were always null under Laravel: the lookup relied on the Symfony-only _route request attribute. Any route($originalRoute, …) call from the view would have failed with Route [] not defined.

Fix

  • SwaggerUiProcessor: read the active route name via Laravel's $request->route()?->getName() (with the _api_original_route API Platform attribute still taking precedence), and pass swaggerUiEnabled / redocEnabled / scalarEnabled into the view so the template can decide what to render.
  • swagger-ui.blade.php: render a header <nav class=\"api-platform-ui-switcher\"> whenever more than one UI is enabled, mark the active link with aria-current=\"page\", and tag #swagger-ui with the active UI as a class so the layout can compensate for the fixed 70px header in ReDoc/Scalar (Swagger UI already pads itself via .info).
  • style.css: subtle segmented-control styling for the switcher; padding-top: 70px on #swagger-ui.redoc / #swagger-ui.scalar so their content is not hidden behind the fixed header.

Behavior is opt-in to multiple UIs: if only one UI is enabled the switcher is not rendered.

Tests

src/Laravel/Tests/DocsTest.php covers the default Swagger UI render plus ?ui=redoc and ?ui=scalar, asserting the switcher markup, the active marker (aria-current=\"page\") and the right init script per UI. A separate DocsSingleUiTest overrides the workbench config to enable only Swagger UI and asserts the switcher is not rendered.

$ vendor/bin/phpunit Tests/DocsTest.php Tests/DocsSingleUiTest.php
OK (8 tests, 27 assertions)

Screenshots

Tested locally against a fresh Laravel skeleton with api-platform/laravel installed via path repository.

Swagger UI (default)

01-swagger-ui

ReDoc (?ui=redoc)

02-redoc

Scalar (?ui=scalar)

03-scalar

The Laravel docs page only rendered Swagger UI even when ReDoc and
Scalar were enabled, since the Blade template never surfaced the
`?ui=redoc` / `?ui=scalar` switch present in the Symfony Twig template.
Expose the enabled flags to the view, render a header nav switcher in
parity with Symfony, and fix the `originalRoute` lookup which relied
on the Symfony-only `_route` request attribute and was always null
under Laravel.

Closes api-platform#7682
@abderrahimghazali abderrahimghazali force-pushed the fix/laravel-redoc-discoverability branch from b2a14a6 to ebb78c2 Compare May 1, 2026 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issues with redoc in laravel

1 participant