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

[5.6] Allow for fluent registration of Route Resources to return a RouteCollection #23890

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
23 changes: 22 additions & 1 deletion src/Illuminate/Routing/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class PendingResourceRegistration
*/
protected $registrar;

/**
* The resource's registration status.
*
* @var bool
*/
protected $registered = false;

/**
* The resource name.
*
Expand Down Expand Up @@ -142,13 +149,27 @@ public function middleware($middleware)
return $this;
}

/**
* Register the Resource.
*
* @return \Illuminate\Routing\RouteCollection
*/
public function register()
{
$this->registered = true;

return $this->registrar->register($this->name, $this->controller, $this->options);
}

/**
* Handle the object's destruction.
*
* @return void
*/
public function __destruct()
{
$this->registrar->register($this->name, $this->controller, $this->options);
if (! $this->registered) {
$this->register();
}
}
}
8 changes: 6 additions & 2 deletions src/Illuminate/Routing/ResourceRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(Router $router)
* @param string $name
* @param string $controller
* @param array $options
* @return void
* @return \Illuminate\Routing\RouteCollection
*/
public function register($name, $controller, array $options = [])
{
Expand All @@ -92,9 +92,13 @@ public function register($name, $controller, array $options = [])

$defaults = $this->resourceDefaults;

$collection = new RouteCollection;

foreach ($this->getResourceMethods($defaults, $options) as $m) {
$this->{'addResource'.ucfirst($m)}($name, $base, $controller, $options);
$collection->add($this->{'addResource'.ucfirst($m)}($name, $base, $controller, $options));
}

return $collection;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ public function testCanRegisterResource()
$this->seeMiddleware('resource-middleware');
}

public function testCanAccessRegisteredResourceRoutesAsRouteCollection()
{
$resource = $this->router->middleware('resource-middleware')
->resource('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub')
->register();

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

$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.index'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.create'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.store'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.show'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.edit'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.update'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy'));
}

public function testCanLimitMethodsOnRegisteredResource()
{
$this->router->resource('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub')
Expand Down