gpt4 book ai didi

php - Laravel:命令中的依赖注入(inject)

转载 作者:可可西里 更新时间:2023-10-31 23:45:53 25 4
gpt4 key购买 nike

是否可以在命令 中注入(inject)自定义类

我正在尝试这个:

<?php

namespace vendor\package\Commands;

use Illuminate\Console\Command;
use vendor\package\Models\Log;
use vendor\package\Updates\UpdateStatistics;

class UpdatePublishmentStats extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'vendorname:updatePublishmentStats';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Updates Twitter followers & Facebook page likes';

/**
* Contact implementation
* @var vendor\package\Update\UpdateStatistics
*/
protected $stats;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct(
Log $log,
UpdateStatistics $stats
) {
parent::__construct();
$this->log = $log;
$this->stats = $stats;
}

但是当我尝试这样做时:

public function handle()
{
$this->stats->updateFbStats();

}

我突然得到 Segmentation fault: 11

当我删除 use vendor\package\Updates\UpdateStatistics; 部分时,我没有收到该错误。

那么我在这里做错了什么?不能在命令中使用依赖注入(inject)吗?

最佳答案

您可以在handle 方法中注入(inject)任何服务:

Note that we are able to inject any dependencies we need into the command's handle method.

来源:https://laravel.com/docs/5.8/artisan#command-structure

关于php - Laravel:命令中的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37647614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com