Skip to content

Commit

Permalink
Plural routes by default (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
bertheyman authored Feb 3, 2024
1 parent 1431224 commit 98f6805
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 36 deletions.
6 changes: 3 additions & 3 deletions config/blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@

/*
|--------------------------------------------------------------------------
| Pluralize route names
| Singular route names
|--------------------------------------------------------------------------
|
| By default, Blueprint will use the `kebab-case` of the controller name
| for the route name. If you would like to ensure a plural route name
| for the route name. If you would like to ensure a singular route name
| is used, you may set this to `true`.
|
*/
'plural_routes' => null,
'singular_routes' => null,

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ protected function buildPivotTableDefinition(array $segments, array $models = []
* pivot table segment, it defaults to appending `$table->foreignId(\'' . $foreignKeyColumnName . '\');`
* to the `$definition` string. The function then returns the `$definition` string.
*
* @param string $pivotTableSegment The segment of the pivot table. e.g 'dive_job' it would be 'Dive' or 'Job'.
* @param string $foreignKeyColumnName The name of the foreign key column. e.g 'dive_id' or 'job_id'.
* @param array $models An array of models. e.g ['Dive' => $diveModel, 'Job' => $jobModel].
* @param string $pivotTableSegment The segment of the pivot table. e.g 'dive_job' it would be 'Dive' or 'Job'.
* @param string $foreignKeyColumnName The name of the foreign key column. e.g 'dive_id' or 'job_id'.
* @param array $models An array of models. e.g ['Dive' => $diveModel, 'Job' => $jobModel].
* @return string The foreign key definition. e.g '$table->foreignUlid('dive_id');'
*/
protected function generateForeignKeyDefinition(string $pivotTableSegment, string $foreignKeyColumnName, array $models = []): string
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/RouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function buildRoutes(Controller $controller): string
$routes = '';
$methods = array_keys($controller->methods());
$className = $this->getClassName($controller);
$slug = config('blueprint.plural_routes') ? Str::plural(Str::kebab($controller->prefix())) : Str::kebab($controller->prefix());
$slug = config('blueprint.singular_routes') ? Str::kebab($controller->prefix()) : Str::plural(Str::kebab($controller->prefix()));

foreach (array_diff($methods, Controller::$resourceMethods) as $method) {
$routes .= $this->buildRouteLine($className, $slug, $method);
Expand Down
12 changes: 6 additions & 6 deletions tests/Feature/Generators/RouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public function output_generates_api_routes(): void
}

#[Test]
public function output_generates_routes_with_plural_slug(): void
public function output_generates_routes_with_singular_slug(): void
{
$this->app['config']->set('blueprint.plural_routes', true);
$this->app['config']->set('blueprint.singular_routes', true);

$this->filesystem->expects('append')
->with('routes/web.php', $this->fixture('routes/readme-example-plural.php'));
->with('routes/web.php', $this->fixture('routes/readme-example-singular.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/readme-example.yaml'));
$tree = $this->blueprint->analyze($tokens);
Expand All @@ -82,12 +82,12 @@ public function output_generates_routes_with_plural_slug(): void
}

#[Test]
public function output_generates_api_routes_with_plural_slug(): void
public function output_generates_api_routes_with_singular_slug(): void
{
$this->app['config']->set('blueprint.plural_routes', true);
$this->app['config']->set('blueprint.singular_routes', true);

$this->filesystem->expects('append')
->with('routes/api.php', $this->fixture('routes/api-routes-plural.php'));
->with('routes/api.php', $this->fixture('routes/api-routes-singular.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml'));
$tree = $this->blueprint->analyze($tokens);
Expand Down
3 changes: 0 additions & 3 deletions tests/fixtures/routes/api-routes-plural.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/fixtures/routes/api-routes-singular.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


Route::apiResource('certificate', App\Http\Controllers\Api\CertificateController::class);
2 changes: 1 addition & 1 deletion tests/fixtures/routes/api-routes.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@


Route::apiResource('certificate', App\Http\Controllers\Api\CertificateController::class);
Route::apiResource('certificates', App\Http\Controllers\Api\CertificateController::class);
2 changes: 1 addition & 1 deletion tests/fixtures/routes/cruddy.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


Route::resource('crud', App\Http\Controllers\CrudController::class);
Route::resource('cruds', App\Http\Controllers\CrudController::class);

Route::resource('users', App\Http\Controllers\UsersController::class)->except('create', 'store');
2 changes: 1 addition & 1 deletion tests/fixtures/routes/invokable-controller.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@


Route::get('report', App\Http\Controllers\ReportController::class);
Route::get('reports', App\Http\Controllers\ReportController::class);
4 changes: 2 additions & 2 deletions tests/fixtures/routes/multiple-resource-controllers-api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


Route::apiResource('file', App\Http\Controllers\FileController::class)->except('index', 'destroy');
Route::apiResource('files', App\Http\Controllers\FileController::class)->except('index', 'destroy');

Route::apiResource('gallery', App\Http\Controllers\GalleryController::class);
Route::apiResource('galleries', App\Http\Controllers\GalleryController::class);
4 changes: 2 additions & 2 deletions tests/fixtures/routes/multiple-resource-controllers-web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


Route::resource('page', App\Http\Controllers\PageController::class);
Route::resource('pages', App\Http\Controllers\PageController::class);

Route::resource('category', App\Http\Controllers\CategoryController::class)->only('index', 'destroy');
Route::resource('categories', App\Http\Controllers\CategoryController::class)->only('index', 'destroy');
8 changes: 4 additions & 4 deletions tests/fixtures/routes/non-cruddy.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


Route::get('some/whatever', [App\Http\Controllers\SomeController::class, 'whatever']);
Route::get('some/slug-name', [App\Http\Controllers\SomeController::class, 'slugName']);
Route::resource('some', App\Http\Controllers\SomeController::class)->only('index', 'show');
Route::get('somes/whatever', [App\Http\Controllers\SomeController::class, 'whatever']);
Route::get('somes/slug-name', [App\Http\Controllers\SomeController::class, 'slugName']);
Route::resource('somes', App\Http\Controllers\SomeController::class)->only('index', 'show');

Route::get('subscriptions/resume', [App\Http\Controllers\SubscriptionsController::class, 'resume']);

Route::get('report', App\Http\Controllers\ReportController::class);
Route::get('reports', App\Http\Controllers\ReportController::class);
3 changes: 0 additions & 3 deletions tests/fixtures/routes/readme-example-plural.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/fixtures/routes/readme-example-singular.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


Route::resource('post', App\Http\Controllers\PostController::class)->only('index', 'store');
2 changes: 1 addition & 1 deletion tests/fixtures/routes/readme-example.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@


Route::resource('post', App\Http\Controllers\PostController::class)->only('index', 'store');
Route::resource('posts', App\Http\Controllers\PostController::class)->only('index', 'store');
4 changes: 2 additions & 2 deletions tests/fixtures/routes/respond-statements.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Route::get('post/error', [App\Http\Controllers\Api\PostController::class, 'error']);
Route::resource('post', App\Http\Controllers\Api\PostController::class)->only('index', 'store');
Route::get('posts/error', [App\Http\Controllers\Api\PostController::class, 'error']);
Route::resource('posts', App\Http\Controllers\Api\PostController::class)->only('index', 'store');
6 changes: 3 additions & 3 deletions tests/fixtures/routes/routes-mixed.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


Route::resource('foo', App\Http\Controllers\FooController::class);
Route::resource('foos', App\Http\Controllers\FooController::class);

Route::get('some/whatever', [App\Http\Controllers\SomeController::class, 'whatever']);
Route::get('somes/whatever', [App\Http\Controllers\SomeController::class, 'whatever']);

Route::get('report', App\Http\Controllers\ReportController::class);
Route::get('reports', App\Http\Controllers\ReportController::class);

0 comments on commit 98f6805

Please sign in to comment.