目标不可实例化. Laravel 5-应用程序绑定服务提供商 [英] Target is not instantiable. Laravel 5 - App binding service provider

查看:120
本文介绍了目标不可实例化. Laravel 5-应用程序绑定服务提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误:

BindingResolutionException in compiled.php line 1029:

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

我的代码如下:

接口:

namespace App\Models\Contracts\Repositories;

use App\Models\Objects\DTO\User;

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

混凝土:

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 ),
                ] );
    }

}

服务提供商:

<?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'
            );
    }

}

控制器:

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 )
    {
    ...

文件夹结构

我还看到我可能需要在我的composer.json中声明名称空间,因此我尝试了以下内容以及上述内容:

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:

"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/"
        }
    },

然后运行composer dump-autoload

有什么主意我忘了做什么?

推荐答案

我注意到,未更新created.php.

I noticed the compiled.php was not being updated.

在项目的根文件夹的cmd行中运行此功能:

Run this function in cmd line on the root folder of your project:

php artisan clear-compiled

这篇关于目标不可实例化. Laravel 5-应用程序绑定服务提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆