Laravel 6 Global variable by reference
I've an application built in Laravel 6. This applications communicate with a VTiger CRM instance via an API.
I've create a model ApiClient, here's the constructor :
function __construct() {
$this->endpointUrl=env("VTIGER_WS_URL");
$this->userName=env("VTIGER_WS_USER");
$this->userKey= env("VTIGER_WS_KEY");
$this->token=env("VTIGER_WS_KEY");
if (!$this->login()) {
echo $this->errorMsg;
}
}
The login method its a curl to the api endpoint, that return me a sessionName, sessionsId and a key. After that, i can make calls to the api an pass those informations for the autorization.
I'm using the Service/Repository pattern to communicatate with VTiger, so i've create multiples services within my application. They all extends a BaseServiceProvider which is :
class BaseServiceProvider
{
protected $apiClient;
public function __construct()
{
$this->apiClient= new ApiClient();
}
}
Everything was working fine until a start noticing that something a got some login failed on the API. Here's an exemple of one of my controller :
class DemoController extends Controller
protected $modelAService;
protected $modelBService;
protected $modelCService;
protected $modelDService;
protected $modelEService;
public function __construct(IModelAService $modelAService, IModelBService $modelBService,
IModelCService $modelCService, IModelDService $modelDService,
IModelEService $modelEService)
{
$this->modelAService= $modelAService;
$this->modelBService= $modelBService;
$this->modelCService= $modelCService;
$this->modelDService= $modelDService;
$this->modelEService= $modelEService;
}
So im inject every service that i need within all of my controllers. So whenever i need a service, i can do : $model = $this->modelAService->get($id); and so on.
And in my service, i do :
public function get($id){
$returnValue = $this->apiClient()->whateverFunctionimplentedForModelA($id);
}
The problem is each of my service create a new ApiClient() variable.
So if in my controller i have a function like this :
public function foo(Request $request) {
$bar = $this->modelAService->whateverfunction($whateverParameter);
$baz = $this->modelBService->whateverfunction($whateverParameter);
$boo = $this->modelCService->whateverfunction($whateverParameter);
$far = $this->modelDService->whateverfunction($whateverParameter);
return whatever !!!
}
When i hit this function, there will be 4 ApiClient() model created, 4 login to the Vtiger API.
What i want to achieve is the only spawn 1 variable ApiClient(), which is created in my BaseServiceProvider , and then each time a service need to ask information to the Vtiger API, the service will access the ApiClient by reference instead of creating a new one each time
I know that i can achieve that with pure php like this exemple, that i use in the past with a connection to database :
$dbConn = new mysql_connection();
public function getData()
{
$this->dbConn->select(blabla...);
}
public function getDataFoo()
{
$this->dbConn->select(blabla...);
}
public function getDataBar()
{
$this->dbConn->select(blabla...);
}
I dont know if its quite clear, but all i want is that all my services only use 1 variable I'm not quite sure if i can accomplished this with Laravel
Thanks you !
Please or to participate in this conversation.