search

Home  >  Q&A  >  body text

laravel - creating provider in larav5 encounters the problem that it cannot be instantiated

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!

伊谢尔伦伊谢尔伦2749 days ago557

reply all(5)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新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

    reply
    0
  • 巴扎黑

    巴扎黑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')

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 16:58:43

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

    reply
    0
  • PHPz

    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

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 16:58:43

    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

    reply
    0
  • Cancelreply