Skip to content

Commit d7e00e7

Browse files
committed
Correct order for supplemental routes
Per the documentation, additional routes to a resource controller should be defined before the call to the `Route::resource` method.
1 parent 9865ae8 commit d7e00e7

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

src/Generators/RouteGenerator.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ protected function buildRoutes(Controller $controller)
5151
{
5252
$routes = '';
5353
$methods = array_keys($controller->methods());
54-
5554
$className = $this->getClassName($controller);
56-
5755
$slug = Str::kebab($controller->prefix());
5856

57+
foreach (array_diff($methods, Controller::$resourceMethods) as $method) {
58+
$routes .= $this->buildRouteLine($className, $slug, $method);
59+
$routes .= PHP_EOL;
60+
}
61+
5962
$resource_methods = array_intersect($methods, Controller::$resourceMethods);
6063
if (count($resource_methods)) {
6164
$routes .= $controller->isApiResource()
@@ -77,12 +80,6 @@ protected function buildRoutes(Controller $controller)
7780
$routes .= ';' . PHP_EOL;
7881
}
7982

80-
$methods = array_diff($methods, Controller::$resourceMethods);
81-
foreach ($methods as $method) {
82-
$routes .= $this->buildRouteLine($className, $slug, $method);
83-
$routes .= PHP_EOL;
84-
}
85-
8683
return trim($routes);
8784
}
8885

tests/Feature/Generators/RouteGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ protected function setUp(): void
3737
*/
3838
public function output_generates_nothing_for_empty_tree()
3939
{
40-
$this->files->shouldNotHaveReceived('append');
41-
4240
$this->assertEquals([], $this->subject->output(new Tree(['controllers' => []])));
41+
42+
$this->files->shouldNotHaveReceived('append');
4343
}
4444

4545
/**

tests/fixtures/routes/non-cruddy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22

3-
Route::resource('some', 'SomeController')->only('index', 'show');
43
Route::get('some/whatever', 'SomeController@whatever');
54
Route::get('some/slug-name', 'SomeController@slugName');
5+
Route::resource('some', 'SomeController')->only('index', 'show');
66

77
Route::get('subscriptions/resume', 'SubscriptionsController@resume');
88

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22

3-
Route::resource('post', 'Api\PostController')->only('index', 'store');
43
Route::get('post/error', 'Api\PostController@error');
4+
Route::resource('post', 'Api\PostController')->only('index', 'store');

0 commit comments

Comments
 (0)