目标不可实例化。 Laravel 5 - 应用绑定服务商
Target is not instantiable. Laravel 5 - App binding service provider
我收到这个错误:
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 中声明名称空间,所以我尝试了以下以及上面的方法:
"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
有什么想法我忘了做什么吗?
我注意到 compiled.php 没有更新。
运行 在您项目的根文件夹的 cmd 行中使用此函数:
php artisan clear-compiled
如果您也 运行 出现以下错误:
BindingResolutionException in Container.php line 749: Target
[App\Contracts\TestContract] is not instantiable.
使用以下命令清除您的配置缓存:
php artisan config:clear
我通过在 app/providers/AppServiceProvider 中的注册方法中添加存储库解决了同样的错误,如下所示。
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->register(RepositoryServiceProvider::class); // here
}
}
我收到这个错误:
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 中声明名称空间,所以我尝试了以下以及上面的方法:
"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
有什么想法我忘了做什么吗?
我注意到 compiled.php 没有更新。
运行 在您项目的根文件夹的 cmd 行中使用此函数:
php artisan clear-compiled
如果您也 运行 出现以下错误:
BindingResolutionException in Container.php line 749: Target [App\Contracts\TestContract] is not instantiable.
使用以下命令清除您的配置缓存:
php artisan config:clear
我通过在 app/providers/AppServiceProvider 中的注册方法中添加存储库解决了同样的错误,如下所示。
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->register(RepositoryServiceProvider::class); // here
}
}