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

[6.x] Add shallow option for resource routes #31208

Merged
merged 3 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Illuminate/Routing/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ public function middleware($middleware)
return $this;
}

/**
* Set the shallow option for a resource.
*
* @param bool $shallow
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function shallow($shallow = true)
{
$this->options['shallow'] = $shallow;

return $this;
}

/**
* Register the resource route.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Routing/ResourceRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ protected function addResourceStore($name, $base, $controller, $options)
*/
protected function addResourceShow($name, $base, $controller, $options)
{
$name = $this->getNameWithShallowness($name, $options);

$uri = $this->getResourceUri($name).'/{'.$base.'}';

$action = $this->getResourceAction($name, $controller, 'show', $options);
Expand All @@ -248,6 +250,8 @@ protected function addResourceShow($name, $base, $controller, $options)
*/
protected function addResourceEdit($name, $base, $controller, $options)
{
$name = $this->getNameWithShallowness($name, $options);

$uri = $this->getResourceUri($name).'/{'.$base.'}/'.static::$verbs['edit'];

$action = $this->getResourceAction($name, $controller, 'edit', $options);
Expand All @@ -266,6 +270,8 @@ protected function addResourceEdit($name, $base, $controller, $options)
*/
protected function addResourceUpdate($name, $base, $controller, $options)
{
$name = $this->getNameWithShallowness($name, $options);

$uri = $this->getResourceUri($name).'/{'.$base.'}';

$action = $this->getResourceAction($name, $controller, 'update', $options);
Expand All @@ -284,6 +290,8 @@ protected function addResourceUpdate($name, $base, $controller, $options)
*/
protected function addResourceDestroy($name, $base, $controller, $options)
{
$name = $this->getNameWithShallowness($name, $options);

$uri = $this->getResourceUri($name).'/{'.$base.'}';

$action = $this->getResourceAction($name, $controller, 'destroy', $options);
Expand Down Expand Up @@ -401,6 +409,22 @@ protected function getResourceRouteName($resource, $method, $options)
return trim(sprintf('%s%s.%s', $prefix, $name, $method), '.');
}

/**
* Get the name for a given resource with shallowness applied when needed.
*
* @param string $name
* @param array $options
* @return string
*/
protected function getNameWithShallowness($name, $options)
{
if (isset($options['shallow']) && $options['shallow']) {
return last(explode('.', $name));
} else {
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
return $name;
}
}

/**
* Set or unset the unmapped global parameters to singular.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@ public function testCanExcludeMethodsOnRegisteredResource()
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy'));
}

public function testCanSetShallowOptionOnRegisteredResource()
{
$this->router->resource('users.tasks', RouteRegistrarControllerStub::class)->shallow();

$this->assertCount(7, $this->router->getRoutes());

$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.tasks.index'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('tasks.show'));
$this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.tasks.show'));
}

public function testCanExcludeMethodsOnRegisteredApiResource()
{
$this->router->apiResource('users', RouteRegistrarControllerStub::class)
Expand Down
35 changes: 35 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,41 @@ public function testInvalidActionException()
$router->dispatch(Request::create('/'));
}

public function testShallowResourceRouting()
{
$router = $this->getRouter();
$router->resource('foo.bar', 'FooController', ['shallow' => true]);
$routes = $router->getRoutes();
$routes = $routes->getRoutes();

$this->assertSame('foo/{foo}/bar', $routes[0]->uri());
$this->assertSame('foo/{foo}/bar/create', $routes[1]->uri());
$this->assertSame('foo/{foo}/bar', $routes[2]->uri());

$this->assertSame('bar/{bar}', $routes[3]->uri());
$this->assertSame('bar/{bar}/edit', $routes[4]->uri());
$this->assertSame('bar/{bar}', $routes[5]->uri());
$this->assertSame('bar/{bar}', $routes[6]->uri());

$router = $this->getRouter();
$router->resource('foo', 'FooController');
$router->resource('foo.bar.baz', 'FooController', ['shallow' => true]);
$routes = $router->getRoutes();
$routes = $routes->getRoutes();

$this->assertSame('foo', $routes[0]->uri());
$this->assertSame('foo/create', $routes[1]->uri());
$this->assertSame('foo', $routes[2]->uri());
$this->assertSame('foo/{foo}', $routes[3]->uri());
$this->assertSame('foo/{foo}/edit', $routes[4]->uri());
$this->assertSame('foo/{foo}', $routes[5]->uri());
$this->assertSame('foo/{foo}', $routes[6]->uri());

$this->assertSame('foo/{foo}/bar/{bar}/baz', $routes[7]->uri());
$this->assertSame('foo/{foo}/bar/{bar}/baz/create', $routes[8]->uri());
$this->assertSame('foo/{foo}/bar/{bar}/baz', $routes[9]->uri());
}

public function testResourceRouting()
{
$router = $this->getRouter();
Expand Down