DEV Community

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

Posted on • Edited on

16 2

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

Image of Quadratic

AI spreadsheet assistant for easy data analysis

Chat with your data and get insights in seconds with the all-in-one spreadsheet that connects to your data, supports code natively, and has built-in AI.

Try Quadratic free

Top comments (2)

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).

Collapse
 
kamalhinduja profile image
Kamal Hinduja

Thanks for sharing use cases and samples of Laravel Topic.

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay