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!
曾经蜡笔没有小新2017-05-16 16:58:43
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
巴扎黑2017-05-16 16:58:43
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')
phpcn_u15822017-05-16 16:58:43
I just want to ask a little question
Can interfaces be instantiated?
PHPz2017-05-16 16:58:43
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
过去多啦不再A梦2017-05-16 16:58:43
Brother wonder~, has this problem been solved? I'm about to collapse