Laravel 4.2:疑难解答“Target not instantiable”错误

时间:2015-02-05 14:45:26

标签: php laravel ioc-container instantiation

过去2-3天我一直坚持这个“Target not instantiable”错误,我不明白为什么。昨天在IRC #laravel聊天室的某个人建议在Artisan的修补程序界面中使用var_dump(App::make('Project\Frontend\Repo\Lead\LeadInterface'))我已经完成并得到了这个回复:

class Project\Frontend\Repo\Lead\EloquentLead#798 (1) {
  protected $lead =>
  class Lead#809 (21) {
    // bunch of info about the model etc relating to the interface and it's repo class
  }
}

#laravel中的一个成员指出这意味着接口已实例化,这很好但后来我意识到这不是我遇到问题的接口。

以下是我设置的方法。显然未实例化的接口是ValidableInterface(代码的最后一部分)并运行var_dump(App::make('Project\Backend\Service\Validation\ValidableInterface')) 返回可怕的“Target not instantiable”错误。

编辑:new LeadFormLaravelValidator( $app['validator'] )扩展了实现ValidableInterface的AbstractLaravelValidator。

我的代码错过了一些明显的问题吗?

我的服务提供商

<?php namespace Project\Frontend\Service\Form;

use Illuminate\Support\ServiceProvider;
use Project\Frontend\Service\Form\Lead\LeadForm;
use Project\Frontend\Service\Form\Lead\LeadFormLaravelValidator;

class FormServiceProvider extends ServiceProvider {

/**
 * Register the binding
 *
 * @return void
 */
public function register()
{
    $app = $this->app;

    $app->bind('Project\Frontend\Service\Form\Lead\LeadForm', function($app)
    {
        return new LeadForm(
            new LeadFormLaravelValidator( $app['validator'] ),
            $app->make('Project\Frontend\Repo\Lead\LeadInterface')
        );
    });
}

}

我的表单类

<?php namespace Project\Frontend\Service\Form\Lead;

use Project\Backend\Service\Validation\ValidableInterface;
use Project\Frontend\Repo\Lead\LeadInterface;

class LeadForm {

/**
 * Form Data
 *
 * @var array
 */
protected $data;

/**
 * Validator
 *
 * @var \Project\Backend\Service\Validation\ValidableInterface
 */
protected $validator;

/**
 * Lead repository
 *
 * @var \Project\Frontend\Repo\Lead\LeadInterface
 */
protected $lead;

public function __construct(ValidableInterface $validator, LeadInterface $lead)
{
    $this->validator = $validator;
    $this->lead = $lead;
}

我的验证规则

<?php namespace Project\Frontend\Service\Form\Lead;

use Project\Backend\Service\Validation\AbstractLaravelValidator;

class LeadFormLaravelValidator extends AbstractLaravelValidator {

/**
 * Validation rules
 *
 * @var Array
 */
protected $rules = array(
    'name'              => 'required|regex:/^[a-zA-Z-\s]+$/',
    'email'             => 'email',
    'cell'              => 'required|numeric|digits_between:10,11',
);

/**
 * Validation messages
 *
 * @var Array
 */
protected $messages = array(
    'regex'                 => 'The :attribute may only contain letters, dashes and spaces.',
    'digits_between'        => 'The :attribute must be 10 numbers long.',
);

}

我的抽象验证器

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

/**
 * Set data to validate
 *
 * @return \Project\Backend\Service\Validation\AbstractLaravelValidator
 */
public function with(array $data)
{
    $this->data = $data;

    return $this;
}

/**
 * Validation passes or fails
 *
 * @return Boolean
 */
public function passes()
{
    $validator = $this->validator->make($this->data, $this->rules, $this->messages);

    if( $validator->fails() )
    {
        $this->errors = $validator->messages();
        return false;
    }

    return true;
}

/**
 * Return errors, if any
 *
 * @return array
 */
public function errors()
{
    return $this->errors;
}

}

我的验证器界面

<?php namespace Project\Backend\Service\Validation;

interface ValidableInterface {

/**
 * Add data to validation against
 *
 * @param array
 * @return \Project\Backend\Service\Validation\ValidableInterface  $this
 */
public function with(array $input);

/**
 * Test if validation passes
 *
 * @return boolean
 */
public function passes();

/**
 * Retrieve validation errors
 *
 * @return array
 */
public function errors();

}

1 个答案:

答案 0 :(得分:1)

我认为问题是$app->make('Project\Frontend\Repo\Lead\LeadInterface')。 Laravel无法知道在这里实例化什么类。你必须告诉Laravel:

$app->bind('Project\Frontend\Repo\Lead\LeadInterface', 'Your\Implementation\Of\LeadInterface');

修改

由于您手动实例化LeadForm并注入LeadFormLaravelValidator,因此您获得该异常很奇怪。但是,这应该可以解决问题:

$app->bind('Project\Backend\Service\Validation\ValidableInterface', 
           'Project\Frontend\Service\Form\Lead\LeadFormLaravelValidator');