Skip to content

Cached health endpoint not working when in maintenance mode #53943

@sanfair

Description

@sanfair

Laravel Version

11.35.0

PHP Version

8.3.14

Database Driver & Version

No response

Description

Follow up for #53772

This implementation does work, but only if you don't use php artisan optimize or php artisan route:cache

If you cache routes, then route definitions are restored from the cache. (check the first code snipped in the spoiler below)

And the closure that eventually excludes a health route from maintenance mode will never be executed.

Closure execution (spoiler)

if ($this->routesAreCached()) {
$this->loadCachedRoutes();
} else {
$this->loadRoutes();

protected function loadRoutes()
{
if (! is_null(self::$alwaysLoadRoutesUsing)) {
$this->app->call(self::$alwaysLoadRoutesUsing);

public static function loadRoutesUsing(?Closure $routesCallback)
{
self::$alwaysLoadRoutesUsing = $routesCallback;
}

if (is_null($using) && (is_string($web) || is_array($web) || is_string($api) || is_array($api) || is_string($pages) || is_string($health)) || is_callable($then)) {
$using = $this->buildRoutingCallback($web, $api, $pages, $health, $apiPrefix, $then);
}
AppRouteServiceProvider::loadRoutesUsing($using);

protected function buildRoutingCallback(array|string|null $web,
array|string|null $api,
?string $pages,
?string $health,
string $apiPrefix,
?callable $then)
{
return function () use ($web, $api, $pages, $health, $apiPrefix, $then) {
if (is_string($api) || is_array($api)) {
if (is_array($api)) {
foreach ($api as $apiRoute) {
if (realpath($apiRoute) !== false) {
Route::middleware('api')->prefix($apiPrefix)->group($apiRoute);
}
}
} else {
Route::middleware('api')->prefix($apiPrefix)->group($api);
}
}
if (is_string($health)) {
PreventRequestsDuringMaintenance::except($health);

Steps To Reproduce

  1. Run php artisan optimize
  2. Put site in maintenance mode php artisan down
  3. Open health route

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions