diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index 8f165acdfdbf..56ea8298ea44 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -328,8 +328,14 @@ public function apiResources(array $resources) */ public function apiResource($name, $controller, array $options = []) { + $only = ['index', 'show', 'store', 'update', 'destroy']; + + if (isset($options['except'])) { + $only = array_diff($only, (array) $options['except']); + } + return $this->resource($name, $controller, array_merge([ - 'only' => ['index', 'show', 'store', 'update', 'destroy'], + 'only' => $only, ], $options)); } diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index c2a335f7bb8c..fc3ff0fd8198 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -269,6 +269,31 @@ public function testUserCanRegisterApiResource() $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.edit')); } + public function testUserCanRegisterApiResourceWithExceptOption() + { + $this->router->apiResource('users', \Illuminate\Tests\Routing\RouteRegistrarControllerStub::class, [ + 'except' => ['destroy'], + ]); + + $this->assertCount(4, $this->router->getRoutes()); + + $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.create')); + $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.edit')); + $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.destroy')); + } + + public function testUserCanRegisterApiResourceWithOnlyOption() + { + $this->router->apiResource('users', \Illuminate\Tests\Routing\RouteRegistrarControllerStub::class, [ + 'only' => ['index', 'show'], + ]); + + $this->assertCount(2, $this->router->getRoutes()); + + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.index')); + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.show')); + } + public function testCanNameRoutesOnRegisteredResource() { $this->router->resource('comments', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub')