Coding Guidelines
Based on own rearch and:
- Spaties Coding Guidlines.
- https://xqsit.github.io/laravel-coding-guidelines/docs/naming-conventions/
- https://github.com/alexeymezenin/laravel-best-practices
General PHP Rules
Code style must follow PSR-1 and PSR-12. Generally speaking, everything string-like that’s not public-facing should use camelCase. Detailed examples on these are spread throughout the guide in their relevant sections.
Variables
Naming Variables
Use speaking variable names:
// Good
$myTenantBuildings = Building::where('tenant_id', $myTenantId)->get();
// Bad
$b = Building::where('tenant_id', $myTenantId)->get();
When assigning data to variables distiguish between singular and plural ressources. Use plural naming for iterables like arrays or collections and singular naming for single ressources.
// Good
$building = Building::findOrFail($buildingId);
$buildings = Building::get();
// Bad
$buildings = Building::findOrFail($buildingId);
$building = Building::get();
Aliasing classes
When you use classes from user namespaces which have a very general name
like Factory
or Provider
then provide an alias to exactly specify their purpose.
This makes life for future developers more easy because they do not have to search the meaning.
// Good
use Support\DataTables\Factory as DataTablesFactory;
// Bad
use Support\DataTables\Factory;
Modules
Plural naming for module namespaces
Please use plural naming for module folders and namespaces if a plural form exists.
// Good
src/Domain/Buildings/
src/Domain/Rooms/
src/Domain/Core/
src/Domain/Reporting/
// Bad
src/Domain/Building/
Separate service provider for all modules
Each module must provide it’s own service provider where the ressources provided by the module are beeing registered. This applies to vertical modularization in terms of layer modules but also to horizontal modularization when a layer is vertically subdivided into multiple modules.
Example:
// src/Domain/DomainServiceProvider.php
// src/Domain/Buildings/BuildingsServiceProvider.php
// etc.
In the shown example the DomainServiceProvider.php
must register the BuildingsServiceProvider.php
.
Routes
Follow REST Resource Naming standard whenever possible
Please use plural naming and dashed-names for routes and route names if a plural form exists. Use snake_case naming for route parameters.
See REST Resource Naming Guide and comment on laracats.com for details.
// Good
Route::resource('buildings', BuildingController::class);
Route::resource('buildings.rooms', Rooms::class);
Route::get('/buildings/tabulate', [BuildingsController::class, 'tabulate'])->name('buildings.tabulate');
Route::get('/training-courses/{training_course}', [BuildingsController::class, 'show'])->name('training-courses.show');
Route::get('/training-courses/{training_course}/files', [TrainingCoursesController::class, 'show'])->name('training-courses.files.index');
Route::get('/my-profile', [ProfileController::class, 'show'])->name('profile.my-profile');
// Bad
Route::resource('building', Building::class);
Route::get('/building/tabulate', [BuildingsController::class, 'tabulate'])->name('building.tabulate');
Route::get('/trainingCourses/{trainingCourse}', [TrainingCoursesController::class, 'show'])->name('trainingCourses.show');
Use camelCase style in controllers when refering to route parameters:
// controller method for the following route:
// Route::get('/training-courses/{training_course}', [BuildingsController::class, 'show'])->name('training-courses.index');
class TrainingCoursesController
{
public function show(TrainingCourse $trainingCourse)
{
// ToDO
}
}
Controllers
Naming
Please use plural naming for controllers if a plural form exists and append the suffix Controller
.
// Good
class BuildingsController
{
//
}
// Bad
class Building
{
//
}
Commands
Naming
Please append the suffix Command
to a commands class name.
// Good
class ImportUsersCommand
{
//
}
// Bad
class ImportUser
{
//
}
Permission Strings
Please use plural naming for permission strings if a plural form exists. Prefix all permission strings with the namespace of the coresponding translation.
// Good
bc-core-domain::roles.read
bc-core-domain::mytenant.events.read
// Bad:
bc-core-domain::role.read
mytenant.role.read
Translation Strings
Please use plural naming for translation strings if a plural form exists.
// Good
{{ __('app.admin.buildings::rooms.headlines.index.main') }}
// Bad:
{{ __('app.admin.building::room.headlines.index.main') }}
Views
Naming views
Files and folders for organizing views must use kebab case for naming. Folders grouping views which represent ressources must use plural naming.
// Good
training-courses/create.blade.php
users/reset-password.blade.php
// Bad:
trainingCourses/create.blade.php
user/reset_password.blade.php
This decision was made based on a research on other popular Laravel projects:
- illuminate/pagination (Kebab case)
- Flarum (kebab case)
- Lavalite (kebab case)
- Faveop (kebab case)
- Invoice-Ninja (snake case)
- Spatie Coding Conventions (camel case)
Form input names
The name attribute for form input elements must follow snake case naming.
// Good
<input type="text" name="date_of_birth">
// Bad
<input type="text" name="dateOfBirth">
<input type="text" name="dateof_Birth">