# Naming Conventions

The following is the generally accepted naming conventions being used by Laravel Community:

**Controllers**

**Controller name MUST start with a noun (in singular form) followed by the word “Controller”.**

**Good**

`class ArticleController extends Controller`&#x20;

`{ ...`

**Bad**

`class ArticlesController extends Controller`

`{...`

`class wp_articlesController extends Controller`

`{`

`class Article extends Controller`

`{`

**You SHOULD Use Resource Controllers unless you have any particular reason not to do so**

**Good**

`class DomainController extends Controller`

`{`

&#x20;`public function index(){} // list domains`

&#x20;`public function create(){} // show create form`

&#x20;`public function store(Request $request){ } // handle the form POST`

&#x20;`public function show($id){} // show a single domain`

&#x20;`public function edit($id){} // show edit page`

&#x20;`public function update(Request $request, $id){} // handle show edit page POST`

&#x20;`public function destroy($id){} // delete a domain`

`}`

**Bad**

`class DomainController extends Controller`

`{`

&#x20;`public function list(){} // list domains`

&#x20;`public function create_or_save(){} // show create form then handle save`

&#x20;`public function show_edit($id){} // show a single domain then show edit page`

&#x20;`public function delete($id){} // delete a domain`

`}`

**Models**

**Model names MUST be in singular form with its first letter in uppercase**

**Good**

`class Flight extends Model`

`{`

`...`

**Bad**

`class Flights extends Model`

`{`

`...`

`class flight extends Model`

`{`

`...`

**hasOne or belongsTo relationship methods MUST be in singular form**

**Good**

`class User extends Model`

`{`

&#x20;`public function phone()`

&#x20;`{`

&#x20;`return $this->hasOne('App\Phone');`

&#x20;`}`

`}`

**Bad**

`class User extends Model`

`{`

&#x20;`public function phones()`

&#x20;`{`

&#x20;`return $this->hasOne('App\Phone');`

&#x20;`}`

`}`

**Any other relationships other than above MUST be in plural form**

**Good**

`class Post extends Model`

`{`

&#x20;`public function comments()`

&#x20;`{`

&#x20;`return $this->hasMany('App\Comment');`

&#x20;`}`

`}`

**Bad**

`class Post extends Model`

`{`

&#x20;`public function comment()`

&#x20;`{`

&#x20;`return $this->hasMany('App\Comment');`

&#x20;`}`

`}`

**Model properties should be in snake\_case**

**Good**

`$user->created_at`

**Bad**

`$user->createdAt`

**Methods should be in camelCase**

**Good**

`class User extends Model`

`{`

&#x20;`public function scopePopular($query)`

&#x20;`{`

&#x20;`return $query->where('votes', '>', 100);`

&#x20;`}`

**Bad**

`class User extends Model`

`{`

&#x20;`public function scope_popular($query)`

&#x20;`{`

&#x20;`return $query->where('votes', '>', 100);`

&#x20;`}`

**Functions**

Laravel comes with a lot of useful helper functions, but you can also define your own helper functions, given the following conditions:

**You SHOULD place your custom helper functions by creating a file called helper.php**

**Good**

`project_folder/app/helper.php`

`project_folder/app/Http/helper.php`

**Bad**

`project_folder/functions.php`

**You MUST use Composer’s autoloading capability to load your functions**

**Good**

// file composer.json

`...`

`"autoload": {`

&#x20;`"files": [`

&#x20;`"app/helpers.php"`

&#x20;`],`

`...`

**Bad**

`// file app/Http/Controllers/HomeController.php`

`class HomeController.php`

`{`

&#x20;`function index(){`

&#x20;`require_once(app_path("helpers.php"));`

&#x20;`}`

`}`

**You MUST check if the the function exists before defining it**

**Good**

`if (! function_exists('my_custom_helper')) {`

&#x20;`function my_custom_helper($key, $default = null) {`

&#x20;`// ...`

&#x20;`}`

`}`

**Bad**

`function my_custom_helper($key, $default = null) {`

&#x20;`// ...`

`}`

**Other General guides with functions**

* If the function length exceeds 25 lines, you SHOULD break it down to multiple functions
* Each function SHOULD have a **Unit Test** associated with it

**Routes**

**Routes should be in plural form of the resource it is trying to manipulate and SHOULD be all lower-case**

**Good**

`Route::get('/users', 'UserController@index');`

`Route::resource('photos', 'PhotoController');`

**Bad**

`Route::get('/user', 'UserController@index');`

`Route::get('/UsersList', 'UserController@index');`

`Route::resource('PHOTO', 'PhotoController');`

**Named Routes SHOULD use snake\_case and dot notation**

**Good**

`Route::get('/user', 'UserController@active')->name('users.show_active');`

**Bad**

`Route::get('/user', 'UserController@active')->name('users.show-active');`

`Route::get('/user', 'UserController@active')->name('show-active-users');`

**Variables**

**General rule for variable is it SHOULD be in camelCase**

**Good**

`$articlesWithAuthor`

**Bad**

`$articles_with_author`

**Collection names SHOULD be descriptive and in plural form**

**Good**

`$activeUsers = User::active()->get()`

**Bad**

`$users = User::active()->get()`

`$user = User::active()->get()`

`$User = User::active()->get()`

**Single Object SHOULD be descriptive and in singular form**

**Good**

`$activeUser = User::active()->first()`

**Bad**

`$users = User::active()->first()`

**Views**

**You SHOULD use snake\_case as file name of your Blade templates**

**Good**

`show_filtered.blade.php`

**Bad**

`showFiltered.blade.php`

`show-filtered.blade.php`

**You MUST not make non UI-related operations inside blade templates**

**Good**

// $api\_results is passed by controller

`<ul>`

&#x20;`@foreach($api_results as $result)`

&#x20;`<li>{{ $result->name }}</li>`

&#x20;`@endforeach`

`</ul>`

**Bad**

`@php`

&#x20;`$api_results = json_decode(file_get_contents("https://api.example.com"));`

`@endphp`

`<ul>`

&#x20;`@foreach($api_results as $result)`

&#x20;`<li>{{ $result->name }}</li>`

&#x20;`@endforeach`

`</ul>`
