-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhow-to-implement-mailchimp-in-dotkernel-api.html.twig
More file actions
128 lines (104 loc) · 4.8 KB
/
Copy pathhow-to-implement-mailchimp-in-dotkernel-api.html.twig
File metadata and controls
128 lines (104 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{% extends '@layout/default.html.twig' %}
{% block json_ld %} {{ include('@jsonld/dotkernel-api/how-to-implement-mailchimp-in-dotkernel-api.jsonld.twig') }} {% endblock %}
{% block title %}How to implement MailChimp in DotKernel API{% endblock %}
{% block page_title %}
{{ include('@partial/title-section.html.twig', {
back_href: url('page::blog'),
back_label: 'Back to Blog',
badge: 'Dotkernel API',
article: article,
}) }}
{% endblock %}
{% block content %}
<div class="wrap post-body-wrap">
<div class="post-layout">
{{ include('@partial/left-menu.html.twig') }}
<article class="article">
<div class="entry">
<h2>This article will walk you through the process of implementing MailChimp into your instance of <a href="https://github.com/dotkernel/api">DotKernel API</a> using <a href="https://github.com/drewm/mailchimp-api">drewm/mailchimp-api</a></h2>
<strong>Step 1</strong>: Add the library to your application using the following command:
<code> composer require drewm/mailchimp-api </code>
<strong>Step 2</strong>: Create configuration file <strong>config/autoload/mailchimp.global.php</strong> and paste the following content inside of it:
<pre><?php
declare(strict_types=1);
return
];
</pre>
<strong>Step 3</strong>: Create factory <strong>src/App/src/MailChimp/Factory/MailChimpFactory.php</strong> which will return an instance of <strong>DrewM\MailChimp</strong>. Paste the following content inside this file:
<pre><?php
declare(strict_types=1);
namespace Api\App\MailChimp\Factory;
use DrewM\MailChimp\MailChimp;
use Psr\Container\ContainerInterface;
/**
* Class MailChimpFactory
* @package Api\App\MailChimp\Factory
*/
class MailChimpFactory
{
/**
* @param ContainerInterface $container
* @return MailChimp
* @throws \Exception
*/
public function __invoke(ContainerInterface $container) : MailChimp
{
$config = $container->get('config')['mailChimp'] ?? [];
return new MailChimp($config['apiKey'] ?? '');
}
}
</pre>
<strong>Step 4</strong>: Let your application use this factory by adding it to the main ConfigProvider:
To do this, open file <strong>src/App/src/ConfigProvider.php</strong> and locate the method called <strong>getDependencies()</strong>.
Inside this method, locate the key <strong>factories</strong> which points to an array. Inside this array add the following line:
<pre>MailChimp::class => MailChimpFactory::class,
</pre>
Make sure you you add the corresponding <strong>use</strong>s:
<pre>use Api\App\MailChimp\Factory\MailChimpFactory;
use DrewM\MailChimp\MailChimp;
</pre>
After this, you can start using the library by <strong>@Inject</strong>ing <strong>MailChimp::class</strong> where it's needed.
<h2 id="user-content-faq">Frequently Asked Questions</h2>
<div class="faq-list">
<details class="acc" open>
<summary>Which library does this tutorial use to add MailChimp to Dotkernel API? <span class="chev">+</span></summary>
<div class="acc-body">
<p class="mb-0">The tutorial uses drewm/mailchimp-api, installed with the command composer require drewm/mailchimp-api.</p>
</div>
</details>
<details class="acc">
<summary>Where do you place the MailChimp configuration file? <span class="chev">+</span></summary>
<div class="acc-body">
<p class="mb-0">In config/autoload/mailchimp.global.php, a new configuration file created as part of Step 2.</p>
</div>
</details>
<details class="acc">
<summary>What does the MailChimpFactory class do? <span class="chev">+</span></summary>
<div class="acc-body">
<p class="mb-0">It's a factory, created at src/App/src/MailChimp/Factory/MailChimpFactory.php, that reads the config from the container and returns an instance of DrewM\MailChimp\MailChimp.</p>
</div>
</details>
<details class="acc">
<summary>Where do you register the MailChimp factory so the application can use it? <span class="chev">+</span></summary>
<div class="acc-body">
<p class="mb-0">In src/App/src/ConfigProvider.php, inside the getDependencies() method's factories array, by mapping MailChimp::class to MailChimpFactory::class, plus adding the corresponding use statements for MailChimp and MailChimpFactory.</p>
</div>
</details>
<details class="acc">
<summary>How do you use MailChimp once it's wired up? <span class="chev">+</span></summary>
<div class="acc-body">
<p class="mb-0">By injecting MailChimp::class wherever it's needed, using @Inject.</p>
</div>
</details>
</div>
</div>
{{ include('@partial/post-nav.html.twig') }}
</article>
</div>
</div>
{% endblock %}