So I scoured through Stackoverflow with still no solution to my problem.
I'm trying to inject a dependency into a class, which is fairly simple to do through the constructor, However I want to pass some params to that Dependency. I tried to use service provider with no success. I'm sort of new to this concept so I'm definitely doing something wrong.
I have a class that needs a SoapClient dependency to work. How can I achieve that using the service provider and dependency injection. Btw, I'm using Laravel.
This is what im doing now, instantiating the soap client inside the method:
namespace App\Services\;
Class Foo{
public function __construct()
{
}
public function getSomething($params)
{
$soapClient = new \SoapClient(env('wsdl'));
$result = $soapClient->someMethod($params);
return $result;
}
}
This is sorta what I want to do:
namespace App\Services\;
Class Foo{
public function __construct( \SoapClient $soap)
{
$this->soap = $soap
}
public function getSomething($params)
{
$result = $this->soap->someMethod($params);
return $result;
}
}
But this of course that won't work because SoapClient class needs a wsdl parameter in order to instantiate.
You can add the following code to the register
method in your App\Providers\AppServiceProvider
:
$this->app->bind(\SoapClient::class, function ($app) {
return new \SoapClient(env('wsdl'));
});
Alternatiely, you could run php artisan make:provider SoapServiceProvider
to create a new serice provider, add the above code to the register
method there, and add Illuminate\View\SoapServiceProvider::class
to providers
array in your config/app.php
file.