skip to Main Content

I’ve built a role module (package) where I’ve used the HasRole trait in the User model. Now, I’m trying to remove the HasRole trait from the User model and add it to the role package’s service provider.

I’ve looked around for solutions but haven’t found anything helpful. I stumbled upon a package called Traitor, but I’m unsure about its reliability.

Could you provide any advice on how to achieve this smoothly and reliably? Thank you!

2

Answers


  1. You should not remove your dependencies totally. You need to be depend on abstraction instead of concrete class.

    You are writing monolith application considering separation of concerns.
    You don’t have to worry about separation at service levels.

    If you have concerns about services it’s ok. Welcome to micro services world. You should call your services with protocols like http, gRPC and…

    Login or Signup to reply.
  2. You don’t need any of that, this is very simple to do:

    1. Inside your package, create a Traits directory, and inside it store your traits and assign each trait to the namespace of the package, for example:
    <?php
    
    namespace MyPackageTraits;
    
    trait HasRoles {
        // trait contents goes here
    }
    
    1. Inside your User model, just refer to the trait by its qualified name:
    <?php
    
    namespace AppModels;
    
    class User extends Model {
        use MyPackageTraitsHasRoles;
    }
    

    Or maybe:

    <?php
    
    namespace AppModels;
    
    use MyPackageTraitsHasRoles;
    
    class User extends Model {
        use HasRoles;
    }
    

    And that’s all about it, no need to overcomplicate things

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search