[英]Register Different services using same class in my Laravel 5.7 Jobs?
I have the following class ony my Laravel App:我的 Laravel 应用程序有以下课程:
namespace App\Api\Consumers
class ShoppingCartApiConsumer
{
private $baseUrl="https://shopping.example.com";
private $apiUrl="";
private $headers=[];
public function __construct(string $apiKey, string $shoppingCartId)
{
$this->apiUrl=$baseUrl."/shoppingCart/$shoppingCartId";
$this->headers["Authorization"]="Bearer $apiKey"];
}
// Rest of methods here
}
And I have the following Jobs:我有以下工作:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
class PrivateShoppingListConsumer implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
private $item;
public function __construct(string $item)
{
$this->item=$item
}
public function handle(ShoppingCartApiConsumer $cart):void
{
//Logic Implemented here
}
}
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
class WorkShoppingListConsumer implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
private $item;
public function __construct(string $item)
{
$this->item=$item
}
public function handle(ShoppingCartApiConssumer $cart):void
{
if($item != "computer"){
return;
}
//rest of code here
}
}
The PrivateShoppingListConsumer
needs the following instance of ShoppingCartApiConsumer
PrivateShoppingListConsumer
需要以下ShoppingCartApiConsumer
实例
$config=config('shoppingCart');
$consumer=new ShoppingListConsumer($config['api_key'],$config['private_shopping_cart_id']);
And the WorkShoppingListConsumer
needs the follwoing instance of ShoppingListConsumer
: WorkShoppingListConsumer
需要ShoppingListConsumer
以下实例:
$config=config('shoppingCart');
$consumer=new ShoppingListConsumer($config['api_key'],$config['work_shopping_cart_id']);
The fonfig for the instance initialization is the following:实例初始化的配置如下:
return [
'api_key' => env("API_KEY"),
'private_shopping_cart_id' => env("PRIVATE_SHOPPING_CART_ID");
'work_shopping_cart_id' => env("WORK_SHOPPING_CART_ID");
]
So I want to know how I can pass the following Instances to each job using laravel's Service Container?所以我想知道如何使用 laravel 的服务容器将以下实例传递给每个作业? Because both
WorkShoppingListConsumer
and PrivateShoppingListConsumer
need the appropriate class instance in order my business logic to work.因为
WorkShoppingListConsumer
和PrivateShoppingListConsumer
需要适当的类实例才能使我的业务逻辑正常工作。
In order to do that you need to define a custom service provider based upon documentation and service container documentation for example:为此,您需要根据文档和服务容器文档定义自定义服务提供者,例如:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ShoppingCartApiConsumerProvider extends ServiceProvider
{
public function register():void
{
$config=config('shoppingCart');
$privateConsumer=new ShoppingListConsumer($config['api_key'],$config['private_shopping_cart_id']);
$workConsumer=new ShoppingListConsumer($config['api_key'],$config['work_shopping_cart_id']);
$this->app->bindMethod(PrivateShoppingListConsumer::class.'@handle', function ($job, $app) use ($privateConsumer) {
return $job->handle($privateConsumer);
});
$this->app->bindMethod(WorkShoppingListConsumer::class.'@handle', function ($job, $app) use ($workConsumer) {
return $job->handle($workConsumer);
});
//Append here more jobs on how to be dispatched
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.