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?
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;
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With