Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PrinceMinky's avatar

Traits - Best Practice

I am doing the ACL in Laravel: Roles and Permissions (https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/16)

At the moment I am at the point where I created a trait.

I love the idea of using traits and will get into the habit of it but I don't want to have them all in the "app/" directory.

Would it be better to have a "app/Traits" directory? Or is there somewhere else I can put it?

0 likes
5 replies
d3xt3r's avatar

IMO, there is no best practice associated with this. As the risk of sounding fairly common, it all depends, if your project is fairly simple (small), you could place them in App\traits. If if its medium to big project, should further modularize and place them in folder where it belongs most. Like traits for models should go in App/Models/Traits, for controllers in App/Http/Controllers/Traits and so.

jekinney's avatar

For medium to large projects I use a modular approach I learned in asp.net. So all my basis user stuff (user, roles, permissions etc) go app/Users

Then I have a model, repository (if needed), and traits folder. Blog same way, site stuff (generally static page stuff if a cms style ability is needed). I use traits to tie everything together so they are only connected in one way and at a high level. On large projects each folder may even include controllers, routes and views for a true module use.

Each piece then can be updated, replaced or removed with just updating traits and routes file. This, for me, has proven amazing more so working in a team. During production mock the other required pieces according to scope doc and At the end just put the prices together, test, bug fix and done.

lee__mason's avatar

i generally organize like so:

/app/Models/User/UserModel.php

if i know for sure i may have only 1/2 traits, ill have

/app/Models/User/UserTrait1.php /app/Models/User/UserTrait2.php

if im not sure, or know there will be more i seperate them into a sub folder:

/app/Models/User/Traits/...

and then

/app/Repositories/User/UserRepository.php /app/Repositories/.../Repository.php

It really does depend on your preference, imo the artisan make:model should auto create the new models in at least a /Models folder as generally speaking this is the most common convention.

wells's avatar

@miiikkeyyyy It sounds like you need a talk about naming things talk from Shawn McCool.

http://shawnmc.cool/a-talk-about-naming-things-talk

It is a very fun talk. Around minute 10:22 Shawn brings up the concept of namespacing by type. The goal is simple. You place the objects that are related to each other in the same namespace. This places the files in the same folder. So, in this instance your traits would live in the same namespace as the object that uses them.

2 likes
PrinceMinky's avatar

Thank you, @wells.

And everybody else for their input. I'm taking so much in lately :D

I have opened the link, but will close my laptop down as I have work at 3am in the morning and it's 11pm at the moment. But when I finish work it'll be my first port of call!

Please or to participate in this conversation.