Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类上的注入接口(Laravel包)_Laravel_Laravel 5 - Fatal编程技术网

类上的注入接口(Laravel包)

类上的注入接口(Laravel包),laravel,laravel-5,Laravel,Laravel 5,我正在开发自己的L5软件包来处理付款。为了能够在将来更改支付网关,我正在使用接口 我的界面如下所示: interface BillerInterface { public function payCash(); public function payCreditCard(); } 我也有一个具体的实现,这是理想的支付网关 class Paypal implements BillerInterface { public function payCash() {

我正在开发自己的L5软件包来处理付款。为了能够在将来更改支付网关,我正在使用接口

我的界面如下所示:

interface BillerInterface
{
    public function payCash();

    public function payCreditCard();
}
我也有一个具体的实现,这是理想的支付网关

class Paypal implements BillerInterface
{
    public function payCash()
    {
        // Logic
    }

    public function payCreditCard()
    {
        // Logic
    }
}
Biller类是主类,构造函数方法需要上面的接口,如下所示:

class Biller {

    protected $gateway;

    public function __construct(BillerInterface $gateway)
    {

        $this->gateway = $gateway;
    }

    // Logic

}
最后,我创建了服务提供者,将接口绑定到网关类

public function register()
{
    $this->app->bind(BillerInterface::class, 'Vendor\Biller\Gateways\Paypal');
}
似乎正在工作,但我在尝试实例化Biller类时出错

Biller::__construct() must be an instance of Vendor\Biller\Contracts\BillerInterface, none given
我尝试了以下代码,但似乎不起作用

public function register()
{
    $this->app->bind(BillerInterface::class, 'Vendor\Biller\Gateways\Paypal');

    $this->app->bind(Biller::class, function ($app) {
        return new Biller($app->make(BillerInterface::class));
    });
}

有什么线索吗?

当您将接口绑定到实际类时,尝试用字符串“\Your\Namespace\BillerInterface”替换
BillerInterface::class

当您将接口绑定到实际类时,尝试用字符串“\Your\Namespace\BillerInterface”替换
这就是我所做的在我的应用程序中,它似乎正在工作:

public function register()
{
    $this->app->bind('DesiredInterface', function ($app) {
        return new DesiredImplementationClass(
            $app['em'],
            new ClassMetaData(DesiredClass::class)
        );
    });
}

这就是我在我的应用程序中所做的,它似乎正在工作:

public function register()
{
    $this->app->bind('DesiredInterface', function ($app) {
        return new DesiredImplementationClass(
            $app['em'],
            new ClassMetaData(DesiredClass::class)
        );
    });
}

您正在将接口绑定到服务提供商中的实现。但是依赖关系将只由服务容器来解决,即

class SomeClass
{
    public function __construct(Billing $billing)
    {
        $this->billing = $billing;
    }
}
Laravel的服务容器将读取构造函数方法参数的类型提示,并解析该实例(以及它的任何依赖项)


您将无法直接“新建”
计费
实例(即
$Billing=new Billing
),因为构造函数希望实现
计费接口
,而您没有提供该接口。

您正在将接口绑定到服务提供商中的实现。但是依赖关系将只由服务容器来解决,即

class SomeClass
{
    public function __construct(Billing $billing)
    {
        $this->billing = $billing;
    }
}
Laravel的服务容器将读取构造函数方法参数的类型提示,并解析该实例(以及它的任何依赖项)


您将无法直接“新建”
账单
实例(即
$Billing=new Billing
),因为构造函数希望实现
账单接口
,而您没有提供。

谈论
@MaGnetas
答案

我更喜欢用这种方式将类与接口绑定

public function register()
{
    $this->app->bind(AppointmentInterface::class, AppointmentService::class);
}
这有助于IDE找到类的路径,我们只需单击它就可以跳转到该类

如果我们将类路径作为字符串路径传递,如下所示:

public function register()
{
    $this->app->bind('App\Interfaces\AppointmentInterface', 'App\Services\AppointmentService');
}

当我们点击这个字符串时,IDE找不到类的位置。

谈论
@MaGnetas
答案

我更喜欢用这种方式将类与接口绑定

public function register()
{
    $this->app->bind(AppointmentInterface::class, AppointmentService::class);
}
这有助于IDE找到类的路径,我们只需单击它就可以跳转到该类

如果我们将类路径作为字符串路径传递,如下所示:

public function register()
{
    $this->app->bind('App\Interfaces\AppointmentInterface', 'App\Services\AppointmentService');
}

当我们点击这个字符串时,IDE找不到类的位置。

你是否实例化了
Biller
实例?我正在我的routes文件中进行测试,基本上我尝试“new Biller”并转储以查看其内容。是的,这不起作用。如果您注入
Biller
以便由服务容器(即
public function\uu构造(Biller$Biller)
)解析,或者使用
$Biller=App::make('Biller')
Martin,您的解决方案成功了,那么依赖项将只会被注入。我没有注意到IoC容器上的依赖项解析特性。我尝试在控制器中使用我的Biller类,在构造函数方法中注入该类之后,它开始工作。如何将您的答案设置为解决方案?=)你是否实例化了
Biller
实例?我正在我的routes文件中进行测试,基本上我正在尝试“new Biller”并转储以查看其内容。是的,这不起作用。如果您注入
Biller
以便由服务容器(即
public function\uu构造(Biller$Biller)
)解析,或者使用
$Biller=App::make('Biller')
Martin,您的解决方案成功了,那么依赖项将只会被注入。我没有注意到IoC容器上的依赖项解析特性。我尝试在控制器中使用我的Biller类,在构造函数方法中注入该类之后,它开始工作。如何将您的答案设置为解决方案?=)