From 5bc33bf512c733df2d926cc1f061d39e5451385a Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 4 Dec 2024 19:39:46 +0000 Subject: [PATCH] [5.x] Fix REST API errors when CP route is empty (#11213) --- src/Providers/RouteServiceProvider.php | 24 +++++++++---------- tests/API/APITest.php | 32 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/Providers/RouteServiceProvider.php b/src/Providers/RouteServiceProvider.php index 7e8338b8c9..ed0899f4de 100644 --- a/src/Providers/RouteServiceProvider.php +++ b/src/Providers/RouteServiceProvider.php @@ -105,14 +105,14 @@ private function needsEntryBinding(?Route $route): bool return false; } - if ($this->isCpRoute($route)) { - return true; - } - if ($this->isApiRoute($route)) { return false; } + if ($this->isCpRoute($route)) { + return true; + } + return $this->isFrontendBindingEnabled(); } @@ -191,14 +191,14 @@ private function needsTermBinding(?Route $route): bool return false; } - if ($this->isCpRoute($route)) { - return true; - } - if ($this->isApiRoute($route)) { return false; } + if ($this->isCpRoute($route)) { + return true; + } + return $this->isFrontendBindingEnabled(); } @@ -382,14 +382,14 @@ private function needsRevisionBinding(?Route $route): bool return false; } - if ($this->isCpRoute($route)) { - return true; - } - if ($this->isApiRoute($route)) { return false; } + if ($this->isCpRoute($route)) { + return true; + } + return $this->isFrontendBindingEnabled(); } diff --git a/tests/API/APITest.php b/tests/API/APITest.php index 4b19592702..32ba32298b 100644 --- a/tests/API/APITest.php +++ b/tests/API/APITest.php @@ -383,6 +383,22 @@ public function relationships_are_shallow_augmented() ]); } + #[Test] + public function can_view_entries_when_cp_route_is_empty() + { + Facades\Config::set('statamic.cp.route', ''); + Facades\Config::set('statamic.api.resources.collections', true); + + Facades\Collection::make('pages')->save(); + Facades\Entry::make()->collection('pages')->id('home')->data(['title' => 'Home'])->save(); + + $this->get('/api/collections/pages/entries/home')->assertJson([ + 'data' => [ + 'title' => 'Home', + ], + ]); + } + #[Test] #[DataProvider('userPasswordFilterProvider')] public function it_never_allows_filtering_users_by_password($filter) @@ -528,6 +544,22 @@ public static function termNotFoundProvider() ]; } + #[Test] + public function can_view_terms_when_cp_route_is_empty() + { + Facades\Config::set('statamic.cp.route', ''); + Facades\Config::set('statamic.api.resources.taxonomies', true); + + Facades\Taxonomy::make('topics')->save(); + Facades\Term::make()->taxonomy('topics')->inDefaultLocale()->slug('dance')->data(['title' => 'Dance'])->save(); + + $this->get('/api/taxonomies/topics/terms/dance')->assertJson([ + 'data' => [ + 'title' => 'Dance', + ], + ]); + } + private function makeCollection($handle) { return Facades\Collection::make($handle);