【问题标题】:How to inject dependencies to a laravel job如何将依赖项注入到 laravel 作业中
【发布时间】:2015-10-31 23:17:31
【问题描述】:

我正在从我的控制器向我的队列中添加一个 laravel 作业

$this->dispatchFromArray(
    'ExportCustomersSearchJob',
    [
        'userId' => $id,
        'clientId' => $clientId
    ]
);

我想在实现ExportCustomersSearchJob 类时注入userRepository 作为依赖项。请问我该怎么做?

我有这个,但它不起作用

class ExportCustomersSearchJob extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels, DispatchesJobs;

    private $userId;

    private $clientId;

    private $userRepository;


    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($userId, $clientId, $userRepository)
    {
        $this->userId = $userId;
        $this->clientId = $clientId;
        $this->userRepository = $userRepository;
    }
}

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.1


    【解决方案1】:

    您在handle 方法中注入您的依赖项:

    class ExportCustomersSearchJob extends Job implements SelfHandling, ShouldQueue
    {
        use InteractsWithQueue, SerializesModels, DispatchesJobs;
    
        private $userId;
    
        private $clientId;
    
        public function __construct($userId, $clientId)
        {
            $this->userId = $userId;
            $this->clientId = $clientId;
        }
    
        public function handle(UserRepository $repository)
        {
            // use $repository here...
        }
    }
    

    【讨论】:

    • 如果我有一个带有handle 方法的父作业类和带有从父handle 方法调用的run 方法的子作业类怎么办?我希望能够在 run 方法中在我的子作业类中键入提示存储库,但随后我会在父类中收到关于 too few arguments 的错误。
    • 只使用继承。在最外层的子作业中键入您需要的所有类,并将对象向下传递给父类。例如,您的子作业可能有此 handle(UserRepository $repository, FooService $fooService) 并通过将 FooService 传递给父级开始:parent::__handle($fooService)
    【解决方案2】:

    如果有人想知道如何将依赖项注入handle 函数:

    将以下内容放入服务提供者中

    $this->app->bindMethod(ExportCustomersSearchJob::class.'@handle', function ($job, $app) {
        return $job->handle($app->make(UserRepository::class));
    });
    

    laravel documentation for job

    【讨论】:

    • 当你使用 dispatch() 函数时,Laravel 会自动为你做这件事。无需手动绑定依赖。
    • 是laravel 5之后的新特性吗?
    • 这是 版本 5 以来的工作方式。文档在此处显示通过服务容器进行的注入:laravel.com/docs/5.0/queues#basic-usage 只要在服务容器中注册了类或服务, 然后 Laravel 会自动将其注入到handle() 方法中。 HTTP 控制器方法也是如此。
    【解决方案3】:

    Laravel v5 及更高版本。

    从 Laravel v5 开始,作业中的依赖项由它们自己处理。 文档显示

    “你可以在handle方法上输入任何你需要的依赖,服务容器会自动注入它们”

    所以,现在,你所要做的就是在 Job 的 handle 方法中添加你想要使用的依赖项。例如:

    use From/where/ever/UserRepository;
    
    class test implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        public function __construct()
        {
    //
        }
    
        public function handle(UserRepository $userRepository)
        {
    //        $userRepository can be used now.
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 2017-08-06
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多