Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

namespaces - Laravel 4 target interface is not instantiable

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 [AppModelsInterfacesPostRepositoryInterface] 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

<?php
use AppModelsInterfacesPostRepositoryInterface;
class PostsController extends BaseController {

    public function __construct( PostRepositoryInterface $posts )
    {
        $this->posts = $posts; 
    }

}

PostRepositoryInterface.php

<?php namespace AppModelsInterfaces;
interface PostRepositoryInterface {
    public function all();
    public function find($id);
    public function store($data);
}

EloquentPostRepository.php

<?php namespace AppModelsRepositories;
use AppModelsInterfacesPostRepositoryInterface;
class EloquentPostRepository implements PostRepositoryInterface {

    public function all()
    {
        return Post::all();
            //after above edit it works to this point
            //error: AppModelsRepositoriesPost 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 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...