简体   繁体   English

如何在Laravel 4中解决“目标[接口]不可实现”?

[英]How do I solve “Target [Interface] is not instantiable” in Laravel 4?

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. 我知道接口和抽象类是不可实例化的,所以我知道Laravel不应该尝试实例化我的接口。 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. 但不知何故,它正在努力,我怀疑这可能是一个具有约束力的问题......即使我相信我已经正确绑定它并将其注册为服务提供商。

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;
    }

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 here where Taylor Otwell, the creator of Laravel, explains this and some other things. 我强烈建议你观看视频,其中Laravel的创造者Taylor Otwell解释了这个以及其他一些事情。

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 运行composer update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM