In my app I have a service called "LogService" to log events and other items. I basically need to use this on every controller to log events by users. Instead of having to instantiate this service in each controller, I had two thoughts for accomplishing this.
Option 1: Bind the service into the IoC and then resolve it that way
Option 2: Make a master class with the service in it and then extend it for other classes so they come with the service already bound
I have questions for each of these methods:
Option 1: Is this even possible? If so, would it just be with "App::make()" that it would be called? That way doesn't seem to play too well with IDE's
Option 2: I have done this kind of thing in the past but PHPStorm does not seem to recognize the service from the parent object because it is instantiated by "App::make()" and not through the regular dependency injection.
What would be the best course of action?
Thanks!
You can have it both ways, I think the neatest way would be:
1) Have an interface that describes your class, let's call it
LogServiceInterface2) Create a Service Provider that instantiates your class, like so:
3) Register this service provider in
config/app.phfile:4) Now, in controller you can request the instance of something that implements
LoggerServiceInterfacestraight in the constructor:(Some controller):
This way, you have got an easy way to quickly change the implementation of your service, moreover, Phpstorm can handle this very easily.
You will still be able to use
app()->make()to obtain an instance of your service. This, however, will not be automatically picked up by Phpstorm. But you can help it to understand that, all you need to do is to use@varannotation, see: