Routing
URLs
Public-facing URLs must use kebab-case.
https://www.interaction-design.org/about/people-behind
https://www.interaction-design.org/my-private-profile
Names
Routes MUST have names. Please use the route() helper to generate URLs from named routes. Route names MUST use camelCase.
Route::get('about', [AboutPageController::class, 'index'])->name('about.index');
Blade
<a href="{{ route('about.index') }}">About</a>
Route names SHOULD include the plural form of the resource name and the action: articles.show, articles.delete.
route()
There are a few valid options for using the route() helper with named routes:
Single parameter
// route: '/meetups/{meetupId}'
$meetup = \App\Modules\LocalGroup\Models\Meetup::query()->find($meetupId);
route('meetups.show', $meetup); // GOOD (RECOMMENDED) for routes with a single parameter
route('meetups.show', $meetupId); // GOOD for cases when you don’t have Meetup object but have an ID/key
route('meetups.show', [$meetup]); // BAD, don’t use array syntax without keys for single-parameter routes
route('meetups.show', ['id' => $meetup]); // GOOD (RECOMMENDED)
route('meetups.show', ['id' => $meetupId]); // GOOD
route('meetups.show', ['meetupId' => $meetup->id]); // ERROR: Missing required parameter "id"
2+ required parameters
// route: '/master-classes/{masterclass}/registrations/{registration}'
$registration = \App\Modules\Masterclass\Models\Registration::query()->first();
route('masterclasses.registrations.show', [$registration->masterclass, $registration]);
route('masterclasses.registrations.show', ['masterclass' => $registration->masterclass, 'registration' => $registration]); // GOOD, RECOMMENDED
route('masterclasses.registrations.show', ['registration' => $registration, 'masterclass' => $registration->masterclass]); // BAD, params mixed up (but still working as expected)
route('masterclasses.registrations.show', [$registration->masterclass, 'registration' => $registration]); // BAD: missing first key (Inconsistency)
route('masterclasses.registrations.show', ['masterclass' => $registration->masterclass, $registration]); // BAD: missing second key (Inconsistency)
Method Chaining
When defining routes, use method chaining instead of an array of params:
// GOOD:
Route::get('about', [AboutPageController::class, 'index'])->name('about.index')->middleware(['cache:1day']);
// BAD:
Route::get('about', ['as' => 'about.index', 'uses' => [AboutPageController::class, 'index']])->middleware(['cache:1day']);
Middleware
Use array syntax for Route::middleware()
// GOOD
Route::get('about', [AboutPageController::class, 'index'])->middleware(['cache:1day']);
Route::get('about', [AboutPageController::class, 'index'])->middleware(['cache:1day', 'CORS']);
// BAD
Route::get('about', [AboutPageController::class, 'index'])->middleware('cache:1day', 'CORS');
Route::get('about', [AboutPageController::class, 'index'])->middleware('cache:1day');
Action notation
Controller + action notation
// GOOD
Route::get('about', [AboutPageController::class, 'index']);
// BAD
Route::get('about', 'AboutPageController@index');
Route Parameters
Route parameters SHOULD use camelCase.
Route::get('members/{memberId}', [MembersController::class, 'show']);
Verbs
All routes have an HTTP verb, which is why we put the verb first when defining a route. It makes a group of routes very readable. Any other route options MUST come after it.
// GOOD: all HTTP verbs come first
Route::get('/', [HomeController::class, 'index'])->name('home');
// BAD: HTTP verbs not easily scannable
Route::name('home')->get('/', [HomeController::class, 'index']);
Requests & Responses
Authorization
In addition to providing built-in authentication services, Laravel also provides a simple way to authorize user actions against a given resource. For example, even though a user is authenticated, they may not be authorized to update or delete certain Eloquent models or database records managed by your application. Laravel
