We have the following binding Primitives
use NZTimMailchimpMailchimp;
use ReCaptchaReCaptcha;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
app()->when(Mailchimp::class)
->needs('$apikey')
->give(config('services.mailchimp.apikey'));
app()->when(ReCaptcha::class)
->needs('$secret')
->give(config('services.recaptcha.secret'));
}
}
Recaptcha is working. But with Mailchimp we receive the following error:
use NZTimMailchimpMailchimp;
$mc = app(Mailchimp::class);
"Mailchimp API key is required – use the ‘MC_KEY’ .env value"
class Mailchimp
{
protected MailchimpApi $api;
public function __construct($apikey, $api = null)
{
if (!is_string($apikey)) {
throw new MailchimpException("Mailchimp API key is required - use the 'MC_KEY' .env value");
}
if (is_null($api)) {
$api = new MailchimpApi($apikey);
}
$this->api = $api;
}
}
+++++++
Update, we tried the following update and still have the same issue.
app()->when(Mailchimp::class)
->needs('$apikey')
->giveConfig('services.mailchimp.apikey');
2
Answers
It looks like the NZTimMailchimp package uses the following ServiceProvider.
config('mailchimp.apikey') wasn't set, adding this config value fixed the issue.
When you need to inject a configuration value, you can use the giveConfig method: