Create your own service provider in larave5. When running, the error "Target [App\IServices\TestServiceInterface] is not instantiable." is reported. The code structure is as follows, please help analyze the reason:
I created a directory named: IServices in the app directory to store my service-related interfaces. The implementations are placed in the Services directory.
Then the service providers are placed in the Providers directory.
After completing the above steps, register the service provider in app.php.
Then inject it through the constructor in HomeController, depending on the interface:
During execution, an error that cannot be instantiated was reported.
I don't understand, why can't it be instantiated? I have already implemented this interface. And also registered? Which key link is wrong? Or missed it? I hope someone can give me some advice, thank you!
Post your bug trace and service provider and take a look. Otherwise, no matter how good you are, you won’t be able to guess where you went wrong. My intuition tells me that you probably didn’t add your own provider in app.php
The writing of
is correct, but you have to tell ioc what the implementation of the interface you are bound to is. In l4, it is the
bind
method. I haven’t looked at it carefully in l5, so it should also have this method. Otherwise, laravel will not know what to inject, and an interface of natural light cannot be instantiated.You should find a place to write something like
App::bind('TestServiceInterface', 'TestService')
I just want to ask a little question
Can interfaces be instantiated?
I don’t know why I re-passed composer for a project. I just wrote it once and it was fine. It may be that something was changed in my previous project during the learning process. I haven't found out exactly where the error is. Sorry
Brother wonder~, has this problem been solved? I'm about to collapse
After searching for the answer myself, it turned out that composer was doing something weird. It seems that we need to understand coposer.json