[13.x] Handle api / json routes with Down (Maintenance) command#60595
Conversation
|
Shouldn't {
"message": "Request cannot be processed during maintenance mode. Please try again later."
}? |
The framework already returns appropriate json when maintenance mode is on, but only if you're not setting a custom view or redirect when running the down command (it doesn't hit the framework in these cases regardless of the request type or prefix) My thoughts were - let the framework handle the response if the path is in the ignore option / array. |
|
Instead of adding a new --ignore option, I think Laravel should automatically do the right thing based on the type of request. Custom maintenance pages and redirects are mainly meant for browser visitors, while API or mobile clients usually expect a JSON response. Updated the PR so that browser users still see the custom maintenance page or redirect, but requests asking for JSON get Laravel’s normal JSON 503 maintenance response instead. This avoids making developers configure path patterns like api/*. |
On our application we occasionally use
--renderor--redirectwith maintenance mode during scheduled maintenance. For examplephp artisan down --render=errors.503-scheduledshows a pre-planned maintenance template rather than the application default (unexpected outage).The issue is that the Laravel
storage/framework/maintenance.phpfile always spits that out, regardless if the client (in this case our mobile app) is expecting JSON rather than HTML.This would be the same if a redirect was intended for web and would break API / mobile clients that are expecting JSON.
This PR adds an
--ignore=*option to the DownCommand. Paths or patterns here will skip thestorage/framework/maintenance.phpcatch and instead refer to the frameworkPreventRequestsDuringMaintenancemiddleware. This will only render the template if the request path is not in the ignore array. Tested this works as expected by returning JSON onapi/usersand the custom view on/when I run:Note: This merges both ignore and except into the
data['except'](downfile) to avoid a non-framework Laravel change / upgrade.