我的错误讯息:
Illuminate \ Container \ BindingResolutionException
Target [Project\Backend\Service\Validation\ValidableInterface] is not instantiable.
我知道接口和抽象类不可实例化,所以我知道Laravel不应该尝试实例化我的接口。然而,不知怎的,它正在尝试并且我怀疑这可能是一个具有约束力的问题......即使我相信我已经正确绑定它并将其注册为服务提供商。
我应该提一下,我把这个例子从Chris Fidao"实施Laravel"它几乎相同!
这是我的表单类的前几行:
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;
}
这是我验证器类的前几行:
namespace Project\Backend\Service\Form\Job;
use Project\Backend\Service\Validation\AbstractLaravelValidator;
class JobFormValidator extends AbstractLaravelValidator {
// Includes some validation rules
这是抽象验证器:
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;
}
这是我将所有内容绑定到应用程序的代码:
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']);
});
}
}
然后在app / config / app.php中注册:
.....
'Project\Backend\Service\Validation\ValidationServiceProvider',
....
最后这些是我控制器的前几行:
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;
}
答案 0 :(得分:6)
当通过类型提示将它注入构造函数时,您需要告诉Laravel它应该用于某个接口的实例。
您可以使用bind()
方法(例如,在您的服务提供商中)
$app->bind('JobInterface', 'Job'); // Job being the class you want to be used
我强烈建议你观看the video here,Laravel的创造者Taylor Otwell解释了这个以及其他一些事情。
答案 1 :(得分:1)
首先你需要使用
进行绑定/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');
}
}
完成此更改后
运行composer update