I have this controller to work with MailChimp:
class MailchimpController extends Controller
{
private $MC_API_KEY;
private $MailChimp;
public function __construct()
{
$this->CHIMP_KEY = getenv('CHIMP_KEY');
$this->MailChimp = new MailChimp($this->CHIMP_KEY);
}
public function index()
{
return $this->MailChimp->get('test');
}
public function show($test)
{
return $this->MailChimp->get("test/$test");
}
}
How do I rewrite this constructor to service provider?
Provider@register:
Lets not deal with the 'env' and pull that from the config (for caching reasons).
$this->app->bind(MailChimp::class, function ($app) {
return new MailChimp($app['config']['services']['mailchimp']['key']);
});
Controller constructor:
public function __construct(MailChimp $mailchimp) {
$this->mailchimp = $mailchimp;
}
For a facade you 'could try' to just use a Real-time Facade.
use Facades\SomeNamespace\MailChimp as MailChimp;
public function blah()
{
MailChimp::get(...);
}
In app/Providers/AppServiceProvider.php
in register()
bind class to service container:
$this->app->bind(MailChimp::class, function () {
return new MailChimp(getenv('CHIMP_KEY'));
});
Now in your controller you have 2 options:
First is to resolve it via app helper:
public function __construct()
{
$this->MailChimp = app(MailChimp::class);
}
Or use dependency injection:
public function __construct(MailChimp $mailchimp)
{
$this->MailChimp = $mailchimp;
}