Laravel 4 target interface is not instantiable

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

This is related to this question How to register a namespace in laravel 4 but I believe I got that worked out and namespaces are working now.

There is a new problem I've run into. I believe the error is coming from trying to type hint in the controller constructor and has to do with using namespaces and using ioc.

BindingResolutionException: Target [App\Models\Interfaces\PostRepositoryInterface] is not instantiable. 

The method below worked perfectly until I tried to introduce namespaces. I can remove all the namespaces and put the interface and repositories in the same directory but would like to know how to make namespaces work with this method of using the ioc.

Here are the relevant files.

routes.php

Route::resource('posts', 'PostsController'); 

PostController.php

posts = $posts;      }  } 

PostRepositoryInterface.php

EloquentPostRepository.php

And you can see composer dump-autoload did it's job.

composer/autoload_classmap.php

return array(     'App\\Models\\Interfaces\\PostRepositoryInterface' => $baseDir . '/app/models/interfaces/PostRepositoryInterface.php',     'App\\Models\\Repositories\\EloquentPostRepository' => $baseDir . '/app/models/repositories/EloquentPostRepository.php',      ....     ) 

Any ideas where or what I need to change to make this work with namepaces like it does without them?

Thanks

回答1:

I know this question has already been answered but I'd like to remind future readers that another common cause of this "target interface is not instantiable" error is to have forgotten to register a service provider in app/config/app.php.

This only applies if you're extending the ServiceProvider class, not if you're using App::bind().

I've made that mistake one too many times not to post something about it. So before you go down the lengthy path outlined above, make sure to register those providers!



回答2:

Ok, the short answer: Use the complete namespace in App::bind() to fix the first error. Then in EloquentPostRepository.php because it has a declared namespace it tries to treat any other external class calls as if they are in the same namespace. Adding a simple 'use Post;' lets PHP know that 'Post' is not in the same namespace (App\Models\Repositories). I assume this is because once a namespace is used, other classes by default have a namespace of whatever the class name is. I thought it would be easiest to just re-post all of the code corrected and working.

routes.php

PostController.php

posts = $posts;     } 

EloquentPostRepository.php

PostRepositoryInterface.php

Post.php Nothing relevant here other than showing it has no declared namespace

 'required',         'body' => 'required',         'author_id' => 'required|numeric'     ];      public static $factory = [         'title' => 'string',         'body' => 'text'     ];      public function user()     {         return $this->belongsTo('User', 'author_id');     }  } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!