将Mailjet API v3包装程序集成为Codeigniter库 [英] Integrating Mailjet API v3 wrapper as Codeigniter library
问题描述
如何将 Mailjet API PHP封装集成到我的Codeigniter中安装为库?
How can I integrate the Mailjet API PHP wrapper into my Codeigniter installation as a library?
这是简单的将 Mailjet.php code>应用程序/ libraries 中的code>文件,如下所示初始化Mailjet?
Is it as simple as placing the contents of the repository into application/libraries/Mailjet
and then creating a Mailjet.php
file in application/libraries
which initializes Mailjet like shown below?
require 'Mailjet/vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
请让我知道如果我在正确的轨道。谢谢。
Please let me know if I'm on the right track. Thanks.
推荐答案
是的,你是在正确的轨道。但是您不需要创建CI库。在控制器中使用 Mailjet
存储库库。只需使用CI 文档中所述的作曲家。
Yes, you are on right track. But you don't need to create CI library. Use Mailjet
repository library in controller as well. Just use composer as stated in CI docs.
如果你想让CodeIgniter使用Composer自动加载器,只需将$ config ['composer_autoload']设置为TRUE或application / config / config.php中的自定义路径。
If you want CodeIgniter to use a Composer auto-loader, just set $config['composer_autoload'] to TRUE or a custom path in application/config/config.php.
在CodeIgniter中使用github存储库的一步指导
- 在
APPPATH.'config / config中设置
文件$ config ['composer_autoload'] = TRUE;
php' - 在
APPPATH $ c $中放置所需存储库/项目的
composer.json
c> location - 通过控制台执行
composer install
命令,这将使供应商
和其他相关文件和文件夹 - 在控制器或其他代码中使用它,如下面的示例所示
- Set
$config['composer_autoload'] = TRUE;
inAPPPATH.'config/config.php'
file - Put
composer.json
file with wanted repositories/projects inAPPPATH
location - Do the job with
composer install
command through console which will makevendor
and other related files and folders inside - Use it when needed in controller or in other code as shown in example bellow
示例控制器Mailman.php
example controller Mailman.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
use \Mailjet\Resources;
class Mailman extends CI_Controller
{
private $apikey = 'apy__key__here';
private $secretkey = 'apy__secret__here';
protected $mj = NULL;
public function __construct()
{
// $this->mj variable is becoming available to controller's methods
$this->mj = new \Mailjet\Client($this->apikey, $this->apisecret);
}
public function index()
{
$response = $this->mj->get(Resources::$Contact);
/*
* Read the response
*/
if ($response->success())
var_dump($response->getData());
else
var_dump($response->getStatus());
}
}
如果您明确要使用Mailjet )存储库,请检入文档如何创建自定义库并将其合并代码上面用它。 Personaly我这样使用存储库,以避免不必要的加载和解析足够的库。
If you explicitly want to use Mailjet (or any other) repository through CI library, check in docs how to create custom library and merge this code above with it. Personaly I use repositories this way to avoid unnecessarily loading and parsing sufficient libraries.
这篇关于将Mailjet API v3包装程序集成为Codeigniter库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!