I am working on a team where my mates introduced the service & repository pattern. We work on Lumen micro framework. So from the controller, the call is passed to the service and then to the repository.
We use controller injection. The service and transformer are injected into the controller's constructor. The service has the repository instantiated in its constructor. We are to follow this rule:
Route -> Controller (Service & Transformer) -> Service(Repository) -> Repository(Model).
The values inside the parentheses mean that they will be initialized inside the constructor.
Now, sometimes it is required to use another repository from the current one. Suppose I am on posts and I need to call user. What would be the way to go? Some say we should inject the user repository into the current repository (post). I disagreed because I don't think we should instantiate the user repository when it is not needed. Rather I would pass the user repository from Controller -> Service -> Repository
. Would that be right?
Code: