Answers.Body: <p>Ideally the third party library would be available as a <a href="https://getcomposer.org/" rel="nofollow">composer</a> package (you can check on <a href="http://packagist.org" rel="nofollow">http://packagist.org</a>) and you'd install it via composer.  By default that will put it in a <code>vendor</code> folder in your plugin.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-21T18:04:10.890Z
Answers.LastActivityDate: 2014-11-21T18:04:10.890Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>I second Brad on this, if there is a composer package, use that.</p>
  
  <p>Whether you use a composer package or a library not found on packagist.org you'll still need to load it so that your plugin can make use of it.</p>
  
  <p>Generally speaking, there are plenty of places where you can import/require third party libraries from but my recommendation would be to do so from the <code>init()</code> method in your main <strong>plugin class</strong>. As far as where to put it, I would go with one of these 5 acceptable directory names used most often in no particular order <code>library</code>, <code>common</code>, <code>third</code>, <code>lib</code>, or <code>src</code>. I'll use <code>library</code> in the following example.</p>
  
  <p>Once you've downloaded/installed your third party library, your directory structure might look something like this...</p>
  
  <pre><code>plugin
      PluginClass.php
      controllers
      templates
      ...
      library
          campaignmonitor
      OR
      library
          vendor
              composer
              campaignmonitor
              ...
  </code></pre>
  
  <p>Given the outlined directory structure above, your <code>init()</code> method might look something like this...</p>
  
  <pre class="lang-php prettyprint-override"><code>// PluginClass.php
  public function init()
  {
      require_once './library/campaignmonitor/SomeClass.php';
      require_once './library/campaignmonitor/SomeOtherClass.php';
      ...
      // If using composer
      require_once './library/vendor/autoload.php';
  }
  </code></pre>
  
  <p>I'd also like to point out that <code>Craft::import()</code> can theoretically import using <code>glob</code> as in <code>Craft::import('plugins.pluginname.library.*)</code> which is cool because those don't get required right away but lazy loaded when needed. However, this only works for class in the Craft namespace so you'll have to stick to a method described above: )</p>
  
  <p>Hope that helps!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-23T02:45:26.117Z
Answers.Text: Your answer is way better than mine.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-23T03:48:44.793Z
Answers.Text: You are too kind Brad! Truth is, I wish I could spend a little more time tackling some questions.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-20T23:26:49.623Z
Answers.Text: Is there an updated answer for this for Craft 3?
Answers.UserDisplayName: kmgdev
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-22T23:37:13.133Z
Answers.LastActivityDate: 2016-10-28T20:36:07.400Z
Answers.LastEditDate: 2016-10-28T20:36:07.400Z
Answers.UserDisplayName: Selvin Ortiz
Body: <p>What is the preferred way of loading a third party library in a plugin? I'm trying to develop a plugin to extend SproutForms so that users get signed up to Campaign Monitor. </p>
  
  <p>So far I've got an interface in the CP where users can provide their API keys and List ID's into the database, so now I just need to actually add the CM API library. </p>
  
  <p>I know I can just require it, but I was wondering if there is a better way of doing this? Also where would I store the library, just in pluginname/lib/?</p>
  
Tags: <plugin-development><control-panel>
Title: Plugin Dev - Load third party library
ThreadText: # Plugin Dev - Load third party library
  
  (Asked by: username_0 on 2014-11-21)
  
  What is the preferred way of loading a third party library in a plugin? I'm trying to develop a plugin to extend SproutForms so that users get signed up to Campaign Monitor.
  
  So far I've got an interface in the CP where users can provide their API keys and List ID's into the database, so now I just need to actually add the CM API library.
  
  I know I can just require it, but I was wondering if there is a better way of doing this? Also where would I store the library, just in pluginname/lib/?
  
  **Tags:** plugin-development, control-panel
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2014-11-22. Score: 13 | Accepted answer
  
  I second username_1 on this, if there is a composer package, use that.
  
  Whether you use a composer package or a library not found on packagist.org you'll still need to load it so that your plugin can make use of it.
  
  Generally speaking, there are plenty of places where you can import/require third party libraries from but my recommendation would be to do so from the `init()` method in your main **plugin class**. As far as where to put it, I would go with one of these 5 acceptable directory names used most often in no particular order `library`, `common`, `third`, `lib`, or `src`. I'll use `library` in the following example.
  
  Once you've downloaded/installed your third party library, your directory structure might look something like this...
  
  ```
  plugin
      PluginClass.php
      controllers
      templates
      ...
      library
          campaignmonitor
      OR
      library
          vendor
              composer
              campaignmonitor
              ...
  
  ```
  
  Given the outlined directory structure above, your `init()` method might look something like this...
  
  ```php
  // PluginClass.php
  public function init()
  {
      require_once './library/campaignmonitor/SomeClass.php';
      require_once './library/campaignmonitor/SomeOtherClass.php';
      ...
      // If using composer
      require_once './library/vendor/autoload.php';
  }
  
  ```
  
  I'd also like to point out that `Craft::import()` can theoretically import using `glob` as in `Craft::import('plugins.pluginname.library.*)` which is cool because those don't get required right away but lazy loaded when needed. However, this only works for class in the Craft namespace so you'll have to stick to a method described above: )
  
  Hope that helps!
  
  > **Comments:**
  > **username_1**: Your answer is way better than mine.  
  > **username_2**: You are too kind username_1! Truth is, I wish I could spend a little more time tackling some questions.  
  > **username_3**: Is there an updated answer for this for Craft 3?  
  
  ---
  
  ### Answer by username_1 on 2014-11-21. Score: 3
  
  Ideally the third party library would be available as a [composer](https://getcomposer.org/) package (you can check on <http://packagist.org>) and you'd install it via composer. By default that will put it in a `vendor` folder in your plugin.
  
  
  ---
  
  