我知道这个问题被问了很多遍了,但是没有一个答案对我有帮助。

我在Laravel 5中遇到异常

BindingResolutionException in Container.php line 785:
Target [App\Contracts\CustomModelInterface] is not instantiable.

我所做的没有成功的事情:
  • App\Providers\AppRepositoryProvider提供者中注册app.php
  • php artisan clear-compiled
  • 如果我替换MyService中存储库上的接口(interface),一切都会正常,但是我觉得这是错误的(应该由IoC容器处理吗?)。

  • 结构:
    app
      - Contracts
        - CustomModelInterface.php
      - Models
        - CustomModel.php
      - Repositories
        - CustomModelRepository.php
      - Providers
        - AppRepositoryProvider.php
      - Services
        - MyService.php
    

    App\Contracts\CustomModelInterface.php
    <?php namespace App\Contracts;
    
    interface CustomModelInterface {
        public function get();
    }
    

    App\Repositories\CustomModelRepository.php
    <?php namespace App\Repositories;
    
    use App\Contracts\CustomModelInterface;
    use App\Models\CustomModel;
    
    class CustomModelRepository implements CustomModelInterface {
    
        private $Model;
    
        public function __construct(CustomModel $model) {
            $this->Model = $model;
        }
    
        public function get() {
            return 'result';
        }
    }
    

    App\Services\MyService.php(保持业务逻辑/ Controller 与存储库之间的层)
    <?php namespace App\Services;
    
    use App\Contracts\CustomModelInterface;
    
    class MyService {
    
        private $Model;
    
        public function __construct(CustomModelInterface $customModel) {
            $this->Model= $customModel;
        }
    
        public function getAll() {
            return $this->Model->get();
        }
    }
    

    App\Providers\AppRepositoryProvider.php
    <?php namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    class AppRepositoryProvider extends ServiceProvider {
    
        public function boot() {}
    
        public function register() {
            $models = array(
                'CustomModel'
            );
    
            foreach ($models as $idx => $model) {
                $this->app->bind("App\Contracts\{$model}Interface", "App\Repositories\{$model}Repository");
            }
        }
    }
    

    我的 Controller 看起来像:
    <?php namespace App\Http\Controllers;
    
    use App\Services\MyService;
    
    class SuperController extends Controller {
    
        private $My;
    
        public function __construct(MyService $myService) {
            $this->My = $myService;
        }
    
        public function getDetails() {
            return $this->My->getAll();
        }
    }
    

    composer.json
    "autoload": {
            "classmap": [
                "database"
            ],
            "psr-4": {
                "App\\": "app/",
                "App\\Models\\": "app/Models/",
                "App\\Contracts\\": "app/Contracts/",
                "App\\Repositories\\": "app/Repositories/"
            }
        },
    

    最佳答案

    谢谢大家,但是问题出在我的AppRepositoryProvider中。由于它是绑定(bind)异常,所以显然问题出在绑定(bind):)

    正确的文件是:

    <?php namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    class AppRepositoryProvider extends ServiceProvider {
    
        public function boot() {}
    
        public function register() {
            $models = array(
                'CustomModel',
                'CustomModel2',
                'CustomModel3'
            );
    
            foreach ($models as $model) {
                $this->app->bind("App\Contracts\\{$model}Interface", "App\Repositories\\{$model}Repository");
            }
        }
    }
    

    请注意,我使用的是"App\Contracts\\{$model}Interface"(不转义“{”符号),它会生成正确的字符串App\Contracts\CustomModelInterface而不是App\Contracts\{$model}Interface(带有意外转义)。

    10-06 04:50