Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place traits in Laravel 5?

I am using Laravel 5 and I am confused about where to place traits files in the Laravel 5 directory structure. Should they exist in public, resources or any other directory?

like image 805
Muhammad Saeed Anwar Avatar asked Mar 10 '15 07:03

Muhammad Saeed Anwar


Video Answer


2 Answers

In terms of placement you should treat traits like classes. That means put them inside the app directory. Where you place your traits in there depends on your preference and on the actual purpose of the trait.

Important is that you adjust the namespace of the trait to the directory structure. For example if your trait has something to do with your controllers and you decide to put it inside the app/Http/Controllers folder, then make sure it has the correct namespace, which would be:

namespace App\Http\Controllers; 
like image 109
lukasgeiter Avatar answered Oct 13 '22 00:10

lukasgeiter


I prefer creating a directory in app called Traits. This will enhance the readability and maintainability of code base .

Example:

namespace App\Traits;  use Exception; use Illuminate\Http\Request;      trait myTrait {     //your code here     } 
like image 36
User123456 Avatar answered Oct 12 '22 23:10

User123456