I am having trouble creating a service for my Laravel application. I wish to use this service to easily communicate with an API to perform IO operations. To be specific, my Laravel application needs to communicate with InvoiceNinja. There is an invoiceninja/sdk-php which I am using to facilitate this communication. According to their docs, it is used like this:
require __DIR__ . '/vendor/autoload.php';
use InvoiceNinja\Config as NinjaConfig;
use InvoiceNinja\Models\Client; // Model can be Invoice, Payment, etc
NinjaConfig::setURL('https://ninja.dev/api/v1');
NinjaConfig::setToken('Your Token');
$clients = Client::all();
The code snippet above is an example of using the API to fetch a list of clients. Now, since I am using Laravel, I did not see the need of setting the API URL and Token every time I needed to send a request to the API. And so I decided to use a Service Provider. Below is my code in InvoiceNinjaServiceProvider.php
register method:
$this->app->singleton(NinjaConfig::class, function ($app) {
NinjaConfig::setURL(config('invoice-ninja.API_URL')); // I created a config/invoice-ninja.php which has these variables set
NinjaConfig::setToken(config('invoice-ninja.API_TOKEN'));
});
I then registered this provider in config/app.php
. I thought that would be enough to use the API in my controllers like this:
use InvoiceNinja\Models\Client;
public function index(InvoiceNinjaServiceProvider $invoiceNinja)
{
$clients = Client::all();
dd($clients); // To test that I actually receive data
}
But I get an error Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\ServiceProvider
. I am not very well versed with Service Providers and I am hoping someone out there can point me in the right direction to get this to work.
Thank you. Thank you!