问题描述
如何将 Mailjet API PHP 包装器 集成到我的 Codeigniter 中安装为库?
How can I integrate the Mailjet API PHP wrapper into my Codeigniter installation as a library?
是否像将存储库的内容放到application/libraries/Mailjet
然后在 application/libraries
中创建一个 Mailjet.php
文件来初始化 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 MailjetResources;
$mj = new MailjetClient(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
请让我知道我是否在正确的轨道上.谢谢.
Please let me know if I'm on the right track. Thanks.
推荐答案
是的,您走对了.但是您不需要创建 CI 库.在控制器中也使用 Mailjet
存储库库.只需按照 CI docs 中所述使用 Composer.
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 中的自定义路径.
在 CodeIgniter 中使用 github 仓库的分步说明
- 在
APPPATH.'config/config.php'
中设置$config['composer_autoload'] = TRUE;
文件 - 将带有所需存储库/项目的
composer.json
文件放在APPPATH
位置 - 通过控制台使用
composer install
命令完成这项工作,这将使vendor
和其他相关文件和文件夹在里面 - 在控制器或其他代码中需要时使用它,如下面的示例所示
- 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 MailjetResources;
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 MailjetClient($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());
}
}
如果您明确希望通过 CI 库使用 Mailjet(或任何其他)存储库,请查看 docs 如何创建自定义库并将上面的代码与其合并.我个人以这种方式使用存储库以避免不必要地加载和解析足够的库.
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 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!