From 60c59e8b8f22b6d871f9dc513ae237d349a3ce06 Mon Sep 17 00:00:00 2001 From: pratik bhujel Date: Wed, 16 Sep 2026 09:57:14 +0545 Subject: [PATCH] Safely load api routes only when routes file exists --- bootstrap/app.php | 2 +- tests/Feature/ApiRoutingTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/ApiRoutingTest.php diff --git a/bootstrap/app.php b/bootstrap/app.php index 5304386..b7f2c64 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -8,7 +8,7 @@ return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', - api: __DIR__.'/../routes/api.php', + api: file_exists(__DIR__.'/../routes/api.php') ? __DIR__.'/../routes/api.php' : null, commands: __DIR__.'/../routes/console.php', health: '/up', ) diff --git a/tests/Feature/ApiRoutingTest.php b/tests/Feature/ApiRoutingTest.php new file mode 100644 index 0000000..e0bc32a --- /dev/null +++ b/tests/Feature/ApiRoutingTest.php @@ -0,0 +1,28 @@ +make(Kernel::class)->bootstrap(); + + expect($app)->toBeInstanceOf(Application::class); + } finally { + if (file_exists($backupFile)) { + rename($backupFile, $apiFile); + } + } +}); + +test('api routes are registered when routes api file is present', function () { + $this->getJson('/api/user')->assertStatus(401); +});