Laravel: Naming Convention & Standards - maple-dev-team/docs GitHub Wiki

Table of Contents

PHP Coding standards

Single vs. double quotes

Always use single quotes to define strings and use . to concatenate with variables when is possible.

Exceptions
When you have single quotes inside the string or using variables inside the string is more readable

define() vs. const

Always use const when defining your constants

define() defines constants at run time, while const defines constants at compile time. This gives const a very slight speed edge. define() puts constants in the global scope, although you can include namespaces in your constant name. That means you can’t use define() to define class constants.

PHP Tags

Use to start a PHP file

You don't need to close PHP files with: ?>

Laravel patterns

Storing passwords

One way hash

use Illuminate\Support\Facades\Hash;

$hashedString = Hash::make($plainText);
//or
$hashedString = bcrypt($plainText);

Hash check

use Illuminate\Support\Facades\Hash;

$result = Hash::check($plainText, $stringHash); // returns true/false

references:
https://laravel.com/docs/8.x/hashing#introduction
https://laravel.com/docs/8.x/helpers#method-bcrypt
https://xqsit.github.io/laravel-coding-guidelines/docs/naming-conventions/

Laravel naming conventions


What How Good Bad
Variable camelCase $anyOtherVariable, $somethingMeaningful, $postCount $any_other_variable, $wx, $count
Controller singular ArticleController ArticlesController
Route snake_case and plural post_reports/1 postReport/1
Named route snake_case with dot notation <syntaxhighlight lang="php">Route::post('name1_name2', 'PostController@example')->name('name1.name2');</syntaxhighlight> <syntaxhighlight lang="php" style="border: 1px solid red;">Route::post('name1.name2', 'PostController@example')->name('name1-name2');</syntaxhighlight>
Model singular User Users
Table plural article_comments article_comment, articleComments
⚠️ **GitHub.com Fallback** ⚠️