I would like to understand how automatic dependancy injection works in laravel. I am using this library https://github.com/yedincisenol/dynamic-links . It has a service provider class which binds to the IoC container
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('DynamicLinks', function ($app) {
return new DynamicLinks($app['config']['dynamic-links']);
});
}
And resolving the class gives me an instance of the library class,
$dynamicLinks = resolve('DynamicLinks');
As per documentaion, the class is auto injected if type hinted in the controller method,
Alternatively, and importantly, you may simply "type-hint" the dependency in the constructor of a class that is resolved by the container, including controllers, event listeners, queue jobs, middleware, and more. In practice, this is how most of your objects should be resolved by the container.
this is my controller method:
public function getLink(Request $request, DynamicLinks $dynamicLinks) {
// other stuffs
}
But the problem is, its not getting the configuaration file in the injected instance and throwing ConfigException from the library.
This is the constructor for the class
public function __construct(array $config = null)
{
if ($config == null) {
$config = require_once 'config.php';
}
if ($config == null || !isset($config['api_key']) || strlen($config['api_key']) < 1) {
throw new ConfigException;
}
$this->config = $config;
$this->client = new Client([
'base_uri' => 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=' . $config['api_key']
]);
}
It seems that the class is being instanciated without the config instead of using the instance registered by the provider.
null... also what does your class referenceDynamicLinkspoint to in your controller method?yedincisenol\DynamicLinks\DynamicLinks;class in the library. Thats why$configisnull.