diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index fbb54e8c821a..796b752c1837 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -11,6 +11,13 @@ class PendingResourceRegistration */ protected $registrar; + /** + * The resource's registration status. + * + * @var bool + */ + protected $registered = false; + /** * The resource name. * @@ -142,6 +149,18 @@ 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. * @@ -149,6 +168,8 @@ public function middleware($middleware) */ public function __destruct() { - $this->registrar->register($this->name, $this->controller, $this->options); + if (! $this->registered) { + $this->register(); + } } } diff --git a/src/Illuminate/Routing/ResourceRegistrar.php b/src/Illuminate/Routing/ResourceRegistrar.php index 5209e9e9279a..d1ec79fc1ae9 100644 --- a/src/Illuminate/Routing/ResourceRegistrar.php +++ b/src/Illuminate/Routing/ResourceRegistrar.php @@ -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 = []) { @@ -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; } /** diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 3a239ddcb047..c2a335f7bb8c 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -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')