PHP Laravel 8 - Best Naming Conventions for APIs ⭐⭐⭐⭐⭐
Tags: php, laravel, bestpractices, beginners
Published: 2022-02-18 10:13:10

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](https://www.php-fig.org/psr/) specifications.

> Best combined with [SOLID](https://dev.to/dalelantowork/solid-principles-object-oriented-programming-in-php-3p3e) Principles

> And remember to follow the [KISS] (https://en.wikipedia.org/wiki/KISS_principle) Principle whenever possible


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8z23h1kcspsxmcp35ze.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1p91dcbiwshjrdd15uo.gif)

Below are the standards use cases and samples :

For Classes, Interfaces/Contracts, Traits: use` PascalCase`

**Class:**

```
class AuthController extends Controller
{
    //
}
```
**Interface/Contracts**

```
interface LoginInterface 
{
    /**
}
```
**Traits**

```
trait Audit
{
    /**
}
```

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';
}
```

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

**Functions/Methods**
```
public function refreshToken() : JsonResponse {
        return $this->loginService->refreshToken();
    }
```

**Class Properties**
```
class AuthController extends Controller
{
    // these are the class properties
    protected $loginService;
    protected $logoutService;

}
```

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

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;
}
```
**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']);
        });
    }
```

**Model Fillables**
```
protected $fillable = [
        'first_name',
        'last_name',
        'username',
        'email',
        'password',

    ];
```

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']);
    });
```