Php 在my Laravel 5.7作业中使用同一类注册不同的服务?
我在Laravel应用程序上有以下课程:Php 在my Laravel 5.7作业中使用同一类注册不同的服务?,php,laravel,laravel-5.7,Php,Laravel,Laravel 5.7,我在Laravel应用程序上有以下课程: namespace App\Api\Consumers class ShoppingCartApiConsumer { private $baseUrl="https://shopping.example.com"; private $apiUrl=""; private $headers=[]; public function __construct(string $apiKey, string $shoppingCar
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
}
我有以下工作:
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
}
}
PrivateShoppingListConsumer
需要以下ShoppingCartApiConsumer
$config=config('shoppingCart');
$consumer=new ShoppingListConsumer($config['api_key'],$config['private_shopping_cart_id']);
而WorkShoppingListConsumer
需要以下ShoppingListConsumer
实例:
$config=config('shoppingCart');
$consumer=new ShoppingListConsumer($config['api_key'],$config['work_shopping_cart_id']);
实例初始化的流程如下所示:
return [
'api_key' => env("API_KEY"),
'private_shopping_cart_id' => env("PRIVATE_SHOPPING_CART_ID");
'work_shopping_cart_id' => env("WORK_SHOPPING_CART_ID");
]
因此,我想知道如何使用laravel的服务容器将以下实例传递给每个作业?因为
WorkShoppingListConsumer
和PrivateShoppingListConsumer
都需要适当的类实例,以便我的业务逻辑正常工作。为此,您需要基于和服务容器定义自定义服务提供者,例如:
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
}
}
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
}
}