Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow adding array or string for web and api routes in bootstrap/app.php #51356

Merged
merged 11 commits into from
May 13, 2024
42 changes: 29 additions & 13 deletions src/Illuminate/Foundation/Configuration/ApplicationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public function withBroadcasting(string $channels, array $attributes = [])
* Register the routing services for the application.
*
* @param \Closure|null $using
* @param string|null $web
* @param string|null $api
* @param array|string|null $web
* @param array|string|null $api
* @param string|null $commands
* @param string|null $channels
* @param string|null $pages
Expand All @@ -141,16 +141,16 @@ public function withBroadcasting(string $channels, array $attributes = [])
* @return $this
*/
public function withRouting(?Closure $using = null,
?string $web = null,
?string $api = null,
array|string|null $web = null,
array|string|null $api = null,
?string $commands = null,
?string $channels = null,
?string $pages = null,
?string $health = null,
string $apiPrefix = 'api',
?callable $then = null)
{
if (is_null($using) && (is_string($web) || is_string($api) || is_string($pages) || is_string($health)) || is_callable($then)) {
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);
}

Expand All @@ -174,24 +174,32 @@ public function withRouting(?Closure $using = null,
/**
* Create the routing callback for the application.
*
* @param string|null $web
* @param string|null $api
* @param array|string|null $web
* @param array|string|null $api
* @param string|null $pages
* @param string|null $health
* @param string $apiPrefix
* @param callable|null $then
* @return \Closure
*/
protected function buildRoutingCallback(?string $web,
?string $api,
protected function buildRoutingCallback(array|string|null $web = null,
array|string|null $api = null,
?string $pages,
?string $health,
string $apiPrefix,
?callable $then)
{
return function () use ($web, $api, $pages, $health, $apiPrefix, $then) {
if (is_string($api) && realpath($api) !== false) {
Route::middleware('api')->prefix($apiPrefix)->group($api);
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)) {
Expand All @@ -202,8 +210,16 @@ protected function buildRoutingCallback(?string $web,
});
}

if (is_string($web) && realpath($web) !== false) {
Route::middleware('web')->group($web);
if (is_string($web) || is_array($web)) {
if (is_array($web)) {
foreach ($web as $webRoute) {
if (realpath($webRoute) !== false) {
Route::middleware('web')->group($webRoute);
}
}
} else {
Route::middleware('web')->group($web);
}
}

if (is_string($pages) &&
Expand Down