【问题标题】:Binding with dependency injection not working. Target is not instantiable使用依赖注入绑定不起作用。目标不可实例化
【发布时间】:2014-06-12 06:45:16
【问题描述】:

所以这可能很容易,但我已经走了好几个小时了。错误信息

Illuminate \ Container \ BindingResolutionException
Target [foo\repositories\OrderRepositoryInterface] is not instantiable.

我的界面:

<?php  namespace foo\repositories;

interface OrderRepositoryInterface
{

 public function index(array $dateRange = null, $customerId = null);

}

存储库:

<?php  namespace foo\repositories;
use Carbon\Carbon;
use Order;

class OrderRepository implements OrderRepositoryInterface
{


public function index(array $dateRange = null, $customerId = null)
{
    return 'HI';

}


} 

我的路线文件:

App::bind('foo\repositories\OrderRepositoryInterface.php',
          'foo\repositories\OrderRepository.php');


Route::resource('orders', 'OrdersController');

最后是控制器:

<?php
use foo\repositories\OrderRepositoryInterface;

class OrdersController extends \BaseController
{

protected $order;

/**
 * @param OrderRepositoryInterface $order
 */
public function __construct(OrderRepositoryInterface $order)
{

    $this->order = $order;
}


public function index()
{
    $orders = $this->order->index();
    return $orders;
}

【问题讨论】:

  • 在 App::bind 中绑定时尝试从界面和存储库中删除 .php
  • @seblaze 的儿子......是的,就是这样。我在这件事上花了几个小时。如果您将其放在答案中,我会将其标记为正确。谢谢!
  • @Casey 做出了回应,所以其他人可以知道这一点 :) 很高兴我能提供帮助

标签: php laravel laravel-4


【解决方案1】:

当您在App::bind 中绑定它们时,尝试从接口和存储库中删除 .php More about binding here

App::bind('foo\repositories\OrderRepositoryInterface',
          'foo\repositories\OrderRepository');

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多