Skip to content

Commit 73d8b49

Browse files
Support plural resources (#574)
1 parent 87b100e commit 73d8b49

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

config/blueprint.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@
114114
*/
115115
'use_guarded' => false,
116116

117+
/*
118+
|--------------------------------------------------------------------------
119+
| Pluralize route names
120+
|--------------------------------------------------------------------------
121+
|
122+
| By default, Blueprint will use the `kebab-case` of the controller name for
123+
| for the route name. If you would like to ensure a plural route name is
124+
| used, you may set this to `true`.
125+
|
126+
*/
127+
'plural_routes' => null,
128+
117129
/*
118130
|--------------------------------------------------------------------------
119131
| Generators

src/Generators/RouteGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected function buildRoutes(Controller $controller)
4343
$routes = '';
4444
$methods = array_keys($controller->methods());
4545
$className = $this->getClassName($controller);
46-
$slug = Str::kebab($controller->prefix());
46+
$slug = config('blueprint.plural_routes') ? Str::plural(Str::kebab($controller->prefix())) : Str::kebab($controller->prefix());
4747

4848
foreach (array_diff($methods, Controller::$resourceMethods) as $method) {
4949
$routes .= $this->buildRouteLine($className, $slug, $method);

tests/Feature/Generators/RouteGeneratorTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
*/
1414
class RouteGeneratorTest extends TestCase
1515
{
16-
private $blueprint;
17-
1816
protected $files;
17+
private $blueprint;
1918

2019
/** @var RouteGenerator */
2120
private $subject;
@@ -71,6 +70,38 @@ public function output_generates_api_routes()
7170
$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
7271
}
7372

73+
/**
74+
* @test
75+
*/
76+
public function output_generates_routes_with_plural_slug()
77+
{
78+
$this->app['config']->set('blueprint.plural_routes', true);
79+
80+
$this->filesystem->expects('append')
81+
->with('routes/web.php', $this->fixture('routes/readme-example-plural.php'));
82+
83+
$tokens = $this->blueprint->parse($this->fixture('drafts/readme-example.yaml'));
84+
$tree = $this->blueprint->analyze($tokens);
85+
86+
$this->assertEquals(['updated' => ['routes/web.php']], $this->subject->output($tree));
87+
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function output_generates_api_routes_with_plural_slug()
93+
{
94+
$this->app['config']->set('blueprint.plural_routes', true);
95+
96+
$this->filesystem->expects('append')
97+
->with('routes/api.php', $this->fixture('routes/api-routes-plural.php'));
98+
99+
$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
100+
$tree = $this->blueprint->analyze($tokens);
101+
102+
$this->assertEquals(['updated' => ['routes/api.php']], $this->subject->output($tree));
103+
}
104+
74105
/**
75106
* @test
76107
*/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
Route::apiResource('certificates', App\Http\Controllers\Api\CertificateController::class);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
Route::resource('posts', App\Http\Controllers\PostController::class)->only('index', 'store');

0 commit comments

Comments
 (0)