laravel - creating provider in larav5 encounters the problem that it cannot be instantiated
伊谢尔伦
伊谢尔伦 2017-05-16 16:56:43
0
5
591

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!

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(5)
曾经蜡笔没有小新

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')

phpcn_u1582

I just want to ask a little question
Can interfaces be instantiated?

PHPzhong

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梦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template