DEV Community

Cover image for PHP Laravel 8 - Best Naming Conventions for APIs ⭐⭐⭐⭐⭐
DaleLanto
DaleLanto

Posted on • Edited on

PHP Laravel 8 - Best Naming Conventions for APIs ⭐⭐⭐⭐⭐

This here are the basic naming conventions which are followed by most big tech companies that uses PHP.

This naming conventions is in compliance with Laravel [PHP] coding Standards with PSR specifications.

Best combined with SOLID Principles

And remember to follow the KISS Principle whenever possible

Image description

Listed here are the naming standards:

  • For Classes, Interfaces/Contracts, Traits: use PascalCase
  • For Constants: use TITLE_CASE
  • For Functions/Methods, Class Properties, Variables: use camelCase
  • For Array Indices/Database Field Names/Model Fillables/Model Relations: use lower_snake_case
  • For Routes: use lower-kebab-case

Image description

Below are the standards use cases and samples :

For Classes, Interfaces/Contracts, Traits: usePascalCase

Class:



class AuthController extends Controller
{
    //
}


Enter fullscreen mode Exit fullscreen mode

Interface/Contracts



interface LoginInterface 
{
    /**
}


Enter fullscreen mode Exit fullscreen mode

Traits



trait Audit
{
    /**
}


Enter fullscreen mode Exit fullscreen mode

For Constants: use TITLE_CASE



namespace App\Constants;

class AppConstant {
    const DEFAULT_PAGE = 1;
    const DEFAULT_PAGE_LIMIT = 10;
    const MAX_PAGE_LIMIT = 100;
    const ALPHANUMERIC_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const NUMERIC_CHARACTERS = '0123456789';
    const ALPHA_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    const UPPERCASE_ALPHA_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    const LOWERCASE_ALPHA_CHARACTERS = 'abcdefghijklmnopqrstuvwxyz';
}


Enter fullscreen mode Exit fullscreen mode

For Functions/Methods, Class Properties, Variables: use camelCase

Functions/Methods



public function refreshToken() : JsonResponse {
        return $this->loginService->refreshToken();
    }


Enter fullscreen mode Exit fullscreen mode

Class Properties



class AuthController extends Controller
{
    // these are the class properties
    protected $loginService;
    protected $logoutService;

}


Enter fullscreen mode Exit fullscreen mode

Variables



public function __construct(LoginService $loginService, LogoutService $logoutService) {
        $this->loginService = $loginService;
        $this->logoutService = $logoutService;
    }


Enter fullscreen mode Exit fullscreen mode

For Array Indices/Database Field Names/Model Fillables/Model Relations: use lower_snake_case

Array Indices



foreach($age as $x => $x_value) {
  return $x_value;
}


Enter fullscreen mode Exit fullscreen mode

Database Field Names



 public function up()
    {
        Schema::create('audits', function (Blueprint $table) {
            $table->id();
            $table->string('user_type')->nullable();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->index(['user_id', 'user_type']);
        });
    }


Enter fullscreen mode Exit fullscreen mode

Model Fillables



protected $fillable = [
        'first_name',
        'last_name',
        'username',
        'email',
        'password',

    ];


Enter fullscreen mode Exit fullscreen mode

For Routes: use lower-kebab-case



Route::group(['middleware' => 'auth:api'], function() {
        Route::post('refresh-token', [AuthController::class, 'refreshToken']);
        Route::post('logout', [AuthController::class, 'logout']);
    });


Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
hcancelik profile image
Can Celik

Model fillables needs to match with database columns so that would work fine but any Class that is connected to database like Eloquent model classes, the class properties (camelCase) also needs to match with database columns(lower_snake_case).

Some comments may only be visible to logged-in visitors. Sign in to view all comments.