Php 在作业中实现存储库接口
我试图在作业中实现一个接口,但运气不好。是否可以在公共构造中实现接口/存储库,并在作业的Php 在作业中实现存储库接口,php,laravel,laravel-5,lumen,Php,Laravel,Laravel 5,Lumen,我试图在作业中实现一个接口,但运气不好。是否可以在公共构造中实现接口/存储库,并在作业的handle()方法中使用所述接口 我得到的错误如下: Argument 1 passed to App\Jobs\OrderCreate::__construct() must be an instance of App\Http\Interfaces\OrderInterface, string given, called in /Users/Panoply/Sites/stock-sync/app/Ht
handle()
方法中使用所述接口
我得到的错误如下:
Argument 1 passed to App\Jobs\OrderCreate::__construct() must be an instance of App\Http\Interfaces\OrderInterface, string given, called in /Users/Panoply/Sites/stock-sync/app/Http/Controllers/StockController.php on line 31
下面是我试图实现的基本设置
股票控制员:
public function test(){
dispatch(new OrderCreate('hello'));
}
订单创建作业:
protected $order;
protected $test;
public function __construct(OrderInterface $order, $test)
{
$this->order = $order;
$this->test = $test;
}
public function handle()
{
$this->order->test($this->test);
}
订单存储库:
class OrderRepository implements OrderInterface
{
public function test($data) {
error_log($data);
}
}
订单界面:
public function test($data);
在我的控制器和命令中实现这种模式并没有任何问题,但我似乎无法在工作中使用它 不管怎样,问题是我不应该在
\uu构造()中调用接口,而是在句柄()中调用接口
编辑以获得更详细的解释。
据我所知,Laravel/Lumen作业的\u构造()
仅接受数据,因此在\u构造()
中实现接口将导致抛出上述错误
要在作业中使用接口,需要在handle()
函数中调用接口
例如,以下内容将不适用于某个职务类别:
protected $test;
public function __construct(InterfaceTest $test)
{
$this->test = $test;
}
这是因为作业构造不接收接口,它只接收从dispatch
调用传入的数据。为了在作业中使用您的接口,您需要在handle()
函数中调用接口,然后它将成功并工作,例如:
public function handle(InterfaceTest $test)
{
$test->fn();
}
似乎只有在工作中实施时才会出现这种情况。在大多数情况下,当您需要控制器或命令中的接口时,您将在\u construct()
中实现 请分享您的解决方案,它可以帮助其他人。谢谢@SachinVairagi为任何可能遇到此问题的人提供了更多的解决方案。