Welcome Guest, Not a member yet? Register   Sign In
Integration with mailchimp.
#1
Lightbulb 
(This post was last modified: 01-03-2020, 10:32 PM by Marcos Lopes.)

Hello, how are you ?

I am performing integration with mailchimp, I followed all steps of integration https://github.com/drewm/mailchimp-api.
But I could not.

My problem is that when I call the mailchimp class, it presents the message saying that the class does not exist.

Does anyone know how I can import this mailchimp class for use in some application controller.
Reply
#2

Try putting MailChimp.php into app/ThirdParty, then in your controller: require APPPATH . 'ThirdParty/MailChimp.php';
Reply
#3

And don't forget to add the namespace to the autoloader.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 01-21-2020, 06:57 PM by JNapolitanoIT.)

I would advise against using any require's or require_once's in your codebase. CodeIgniter 4 adheres to PSR-4 and can easily autoload the entire Mailchimp library and Mailchimp classes in the Autoload file.

To get this going you simply need to pass your Mailchimp namespace and location into the $psr4 array to load it into the application as InsiteFX suggested. See below:

In app/Config/Autoload.php:
Code:
// ADD THE DIRECTORY TO THE PSR-4 ARRAY
$psr4 = [
  // Namespace => Directory
  'MailChimp' => 'location/of/mailchimp/src'
];

Now you just call your class like so:

Code:
use Mailchimp\Mailchimp;

$mailchimp = new Mailchimp();

EDIT
I just had a random thought about this. A better idea would probably be to create yourself a mailchimp service and use that, this way you can call this service anywhere in your application to load the Mailchimp library. If you ever need to change out your Mailchimp library, all you need to do is modify the service and every service call will use the new library.

An example implementation would be like what you see below. Mind you this is not tested and is more of a proof-of-concept:

First, download the MailChimp library in the link you provided and put it inside of app/ThirdParty/MailChimp.

Now add the MailChimp library to the Autoloader in app/Config/Autoload.php:
Code:
// ADD THE DIRECTORY TO THE PSR-4 ARRAY
$psr4 = [
  // Namespace => Directory
  'MailChimp' => APPPATH . 'ThirdParty/MailChimp/src' // Located inside of: app/ThirdParty/MailChimp/src
];

Then go to app/Config/Services.php and add the following:

Code:
/**
* A service method for using the mailchimp library
*
* @param bool  $getShared Return a shared instance?
*
 * @return mixed
*/
public function mailchimp(bool $getShared = true)
{
    if (! $getShared)
    {
        $api_key = '';
        return new \MailChimp\MailChimp($api_key);
    }
    return static::getSharedInstance('mailchimp');
}

Now you could of course call it from your application like so:

Code:
$mailchimp = \Config\Services::mailchimp();

return $mailchimp->delete( ... args ... );
return $mailchimp->post( ... args ... );
return $mailchimp->get( ... args ... );

However, I find it to be better to create a helper function and use that. This way you only call \Config\Services::mailchimp() in this one location. See below for an example:

In app/Common.php, create a helper function for grabbing the service method:
Code:
/**
* Returns an instance of the MailChimp service method
*
* @see \Config\Services::mailchimp()
*
* @param bool $getShared Return a shared instance?
*/
function mailchimp($getShared = true)
{
    return \Config\Services::mailchimp($getShared);
}

Now, you can call this procedural function from anywhere like so:
Code:
return mailchimp()->delete( ... args ... );
return mailchimp()->post( ... args ... );
return mailchimp()->get( ... args ... );

I hope this all helps and is not too overwhelming Smile
A reader lives a thousand lives before he dies. The man who never reads lives only one.
George R.R. Martin

Reply




Theme © iAndrew 2016 - Forum software by © MyBB