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

时间:2015-02-18 23:17:38

标签: php 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 )
    {
    ...

文件夹结构

enter image description here

我还看到我可能需要在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/"
        }
    },

然后运行composer dump-autoload

我忘了做什么?

3 个答案:

答案 0 :(得分:17)

我注意到compiled.php没有更新。

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

php artisan clear-compiled

答案 1 :(得分:7)

如果您还遇到以下错误:

  

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

使用以下命令清除配置缓存:

php artisan config:clear

答案 2 :(得分:0)

我通过在 app/providers/AppServiceProvider 中的注册方法中添加存储库解决了同样的错误,如下所示。

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

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