Skip to content

Commit

Permalink
Let apiResource support except option. (#24319)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmichot authored and taylorotwell committed May 25, 2018
1 parent a580df5 commit c4ccd93
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit c4ccd93

Please sign in to comment.