skip to Main Content

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


  1. Chosen as BEST ANSWER

    It looks like the NZTimMailchimp package uses the following ServiceProvider.

    public function register()
        {
            $this->app->bind(Mailchimp::class, function () {
                return new Mailchimp(config('mailchimp.apikey'));
            });
            $this->app->bind('mailchimp', function () {
                return $this->app->make(Mailchimp::class);
            });
            $this->mergeConfigFrom(__DIR__.'/../config/mailchimp.php', 'mailchimp');
        }
    

    config('mailchimp.apikey') wasn't set, adding this config value fixed the issue.


  2. When you need to inject a configuration value, you can use the giveConfig method:

    $this->app->when(Mailchimp::class)
        ->needs('$apikey')
        ->giveConfig('services.mailchimp.apikey');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search