Laravel 4 target interface not working

This is related to this question. How to register a namespace in laravel 4 , but I believe that I have developed this, and namespaces are working now.

There was a new problem that I encountered. I believe that the error comes from trying to type a hint in the controller constructor and is related to using namespaces and using ioc.

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

The method below worked fine until I tried to enter namespaces. I can delete all 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 ioc.

Here are the relevant files.

routes.php

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

PostController.php

 <?php use App\Models\Interfaces\PostRepositoryInterface; class PostsController extends BaseController { public function __construct( PostRepositoryInterface $posts ) { $this->posts = $posts; } } 

PostRepositoryInterface.php

 <?php namespace App\Models\Interfaces; interface PostRepositoryInterface { public function all(); public function find($id); public function store($data); } 

EloquentPostRepository.php

 <?php namespace App\Models\Repositories; use App\Models\Interfaces\PostRepositoryInterface; class EloquentPostRepository implements PostRepositoryInterface { public function all() { return Post::all(); //after above edit it works to this point //error: App\Models\Repositories\Post not found //because Post is not in this namespace } public function find($id) { return Post::find($id); } public function store($data) { return Post::save($data); } } 

And you can see that the composer dump-autoload did the 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 names, how does this happen without them?

thanks

+6
source share
2 answers

Ok, short answer: use the full namespace in App :: bind () to fix the first error. Then in EloquentPostRepository.php, since it has a declared namespace, it tries to handle any other calls to external classes as if they were in the same namespace. Adding a simple "Use Message"; lets PHP know that Mail is not in the same namespace (App \ Models \ Repositories). I assume this is because as soon as the namespace is used, other classes by default have a namespace of any class name. I thought it would be easier to just reinstall all the code, adjusted and working.

routes.php

 <?php App::bind('App\Models\Interfaces\PostRepositoryInterface', 'App\Models\Repositories\EloquentPostRepository'); Route::resource('posts', 'PostsController'); 

PostController.php

 <?php use App\Models\Interfaces\PostRepositoryInterface; class PostsController extends BaseController { public function __construct(PostRepositoryInterface $posts) { $this->posts = $posts; } 

EloquentPostRepository.php

 <?php namespace App\Models\Repositories; use App\Models\Interfaces\PostRepositoryInterface; use Post; class EloquentPostRepository implements PostRepositoryInterface { public function all() { return Post::all(); } public function find($id) { return Post::find($id); } public function store($data) { return Post::save($data); } } 

PostRepositoryInterface.php

 <?php namespace App\Models\Interfaces; interface PostRepositoryInterface { public function all(); public function find($id); public function store($data); } 

Post.php Nothing significant here except displaying it has a declared namespace

 <?php class Post extends BaseModel { public static $rules = [ 'title' => 'required', 'body' => 'required', 'author_id' => 'required|numeric' ]; public static $factory = [ 'title' => 'string', 'body' => 'text' ]; public function user() { return $this->belongsTo('User', 'author_id'); } } 
+13
source

I know this question has already been answered, but I would like to remind future readers that another common cause of this “ target interface is not instantiable ” error is to forget to register the service provider in app/config/app.php .

This only applies if you are extending the ServiceProvider class, and not when using App::bind() .

I have made this mistake too many times to not publish anything. Therefore, before you go along the long path described above, be sure to register these suppliers!

+37
source

All Articles