简体   繁体   English

目标不可实例化。 Laravel 5 - 应用绑定服务提供者

[英]Target is not instantiable. Laravel 5 - App binding service provider

I'm getting this error:我收到此错误:

BindingResolutionException in compiled.php line 1029:

Target [App\Models\Contracts\Repositories\IUserRepository] is not instantiable.

My code is as follows:我的代码如下:

Interface:界面:

namespace App\Models\Contracts\Repositories;

use App\Models\Objects\DTO\User;

interface IUserRepository
{
    function Create( User $user );
}

Concrete:具体的:

namespace App\Models\Concrete\Eloquent;

use App\Models\Contracts\Repositories\IUserRepository;
use App\Models\Objects\DTO\User;

class EqUserRepository implements IUserRepository
{
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    public function Create( User $user )
    {
        return User::create( [
                    'first_name' => $user->first_name,
                    'last_name' => $user->last_name,
                    'username' => $user->username,
                    'email' => $user->email,
                    'password' => bcrypt( $user->password ),
                ] );
    }

}

Service Provider:服务提供者:

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * This service provider is a great spot to register your various container
     * bindings with the application. As you can see, we are registering our
     * "Registrar" implementation here. You can add your own bindings too!
     *
     * @return void
     */
    public function register()
    {

            $this->app->bind(
                    'App\Models\Contracts\Repositories\IUserRepository', 
                    'App\Models\Concrete\Eloquent\EqUserRepository'
            );
    }

}

Controller:控制器:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

use App\Models\Contracts\Repositories\IUserRepository;
use App\Models\Objects\DTO\User;

class AuthController extends Controller
{
    protected $auth;
    private $userRepository;

    public function __Construct(  
            Guard $auth, 
            IUserRepository $userRepo )
    {
    ...

Folder structure文件夹结构

在此处输入图片说明

I have also seen that I may need to declare the namespaces in my composer.json, So i have tried the following as well as just the above:我还看到我可能需要在我的 composer.json 中声明命名空间,所以我尝试了以下以及上面的方法:

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "App\\Models\\Concrete\\Eloquent\\": "app/Models/Concrete/Eloquent/",
            "App\\Models\\Contracts\\Repositories\\": "app/Models/Contracts/Repositories/",
            "App\\Models\\Objects\\DTO\\": "app/Models/Objects/DTO/"
        }
    },

and then ran composer dump-autoload然后运行composer dump-autoload

Any ideas what I am forgetting to do?任何想法我忘记做什么?

I noticed the compiled.php was not being updated.我注意到compiled.php 没有被更新。

Run this function in cmd line on the root folder of your project:在项目根文件夹的 cmd 行中运行此函数:

php artisan clear-compiled

If you also run in below error :如果您还遇到以下错误:

BindingResolutionException in Container.php line 749: Target [App\\Contracts\\TestContract] is not instantiable. Container.php 第 749 行中的 BindingResolutionException:目标 [App\\Contracts\\TestContract] 不可实例化。

Clear your config cache with :使用以下命令清除配置缓存:

php artisan config:clear

I had same error solved by adding repository in app/providers/AppServiceProvider in register method like the below.我通过在 app/providers/AppServiceProvider 中的注册方法中添加存储库解决了同样的错误,如下所示。

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

public function register()
    {
        $this->app->register(RepositoryServiceProvider::class); // here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM