如何解决 Laravel 4 中的“目标 [接口] 不可实例化”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27760392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I solve "Target [Interface] is not instantiable" in Laravel 4?
提问by noobmaster69
My error message:
我的错误信息:
Illuminate \ Container \ BindingResolutionException
Target [Project\Backend\Service\Validation\ValidableInterface] is not instantiable.
I understand that interfaces and abstract classes are not instantiable so I know that Laravel should not be trying to instantiate my interface. Yet somehow it's trying to and I suspect this may be a binding issue...even though I believe I have bound it correctly and have registered it as a service provider.
我知道接口和抽象类是不可实例化的,所以我知道 Laravel 不应该尝试实例化我的接口。然而不知何故,它正在尝试并且我怀疑这可能是一个绑定问题......即使我相信我已经正确绑定它并将其注册为服务提供商。
I should mention that I have taken this example out of Chris Fidao's "Implementing Laravel" and it's almost identical!
我应该提一下,我从 Chris Fidao 的“实现 Laravel”中拿了这个例子,它几乎是一样的!
This is the first couple of lines of my form class:
这是我的表单类的前几行:
namespace Project\Backend\Service\Form\Job;
use Project\Backend\Service\Validation\ValidableInterface;
use Project\Backend\Repo\Job\JobInterface;
class JobForm {
/**
* Form Data
*
* @var array
*/
protected $data;
/**
* Validator
*
* @var \Project\Backend\Form\Service\ValidableInterface
*/
protected $validator;
/**
* Job repository
*
* @var \Project\Backend\Repo\Job\JobInterface
*/
protected $job;
public function __construct(ValidableInterface $validator, JobInterface $job)
{
$this->validator = $validator;
$this->job = $job;
}
This is the first few lines of my validator class:
这是我的验证器类的前几行:
namespace Project\Backend\Service\Form\Job;
use Project\Backend\Service\Validation\AbstractLaravelValidator;
class JobFormValidator extends AbstractLaravelValidator {
// Includes some validation rules
This is the abstract validator:
这是抽象验证器:
namespace Project\Backend\Service\Validation;
use Illuminate\Validation\Factory;
abstract class AbstractLaravelValidator implements ValidableInterface {
/**
* Validator
*
* @var \Illuminate\Validation\Factory
*/
protected $validator;
/**
* Validation data key => value array
*
* @var Array
*/
protected $data = array();
/**
* Validation errors
*
* @var Array
*/
protected $errors = array();
/**
* Validation rules
*
* @var Array
*/
protected $rules = array();
/**
* Custom validation messages
*
* @var Array
*/
protected $messages = array();
public function __construct(Factory $validator)
{
$this->validator = $validator;
}
This is the code where I bind it all to the app:
这是我将其全部绑定到应用程序的代码:
namespace Project\Backend\Service\Validation;
use Illuminate\Support\ServiceProvider;
use Project\Backend\Service\Form\Job\JobFormValidator;
class ValidationServiceProvider extends ServiceProvider {
public function register()
{
$app = $this->app;
$app->bind('Project\Backend\Service\Form\Job\JobFormValidator', function($app)
{
return new JobFormValidator($app['validator']);
});
}
}
This is then registered in app/config/app.php:
然后在 app/config/app.php 中注册:
.....
'Project\Backend\Service\Validation\ValidationServiceProvider',
....
Finally these are the first few lines of my controller:
最后这些是我的控制器的前几行:
use Project\Backend\Repo\Job\JobInterface;
use Project\Backend\Service\Form\Job\JobForm;
class JobController extends \BaseController {
protected $jobform;
function __construct(JobInterface $job, JobForm $jobform)
{
$this->job = $job;
$this->jobform = $jobform;
}
回答by lukasgeiter
You need to tell Laravel which instance it should use for a certain interface when injecting it into the constructor via type hinting.
当通过类型提示将它注入构造函数时,你需要告诉 Laravel 它应该为某个接口使用哪个实例。
You do this using the bind()
method (in your service provider for example)
您使用该bind()
方法执行此操作(例如在您的服务提供商中)
$app->bind('JobInterface', 'Job'); // Job being the class you want to be used
I highly recommend you watch the video herewhere Taylor Otwell, the creator of Laravel, explains this and some other things.
我强烈建议你在这里观看Laravel 的创造者 Taylor Otwell 解释这个和其他一些事情的视频。
回答by Virat Gaywala - CSM
First you need to bind using
首先你需要使用绑定
/app/Providers/AppServiceProvider.php
/app/Providers/AppServiceProvider.php
<?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.
*
* @return void
*/
public function register()
{
//
$this->app->bind('JobInterface', 'Job');
}
}
Once you complete this change
完成此更改后
Run composer update
运行作曲家更新