Php Codeigniter使用library或helper使发送电子邮件干燥?

Php Codeigniter使用library或helper使发送电子邮件干燥?,php,codeigniter,email,codeigniter-3,codeigniter-email,Php,Codeigniter,Email,Codeigniter 3,Codeigniter Email,我正试着在Codeigniter中使用自定义库、自定义帮助程序和常规旧控制器的时间。据我所知,定制助手应该更像是完成简单重复任务的小函数,而库可以是一个成熟的类 例如,我希望在CI中使用本机电子邮件类,但我认为我将在各种不同的控制器中使用它,并希望它尽可能“干燥”。将代码抽象为向助手或库发送电子邮件是否有意义?或者我应该在需要的控制器中重复它吗?我还有一个基本控制器。也许我应该把代码放在那里?它的使用频率足以重复,但并不是每个控制器都在使用它 根据文档,我希望抽象的重复代码类似于以下内容: 下面

我正试着在Codeigniter中使用自定义库、自定义帮助程序和常规旧控制器的时间。据我所知,定制助手应该更像是完成简单重复任务的小函数,而库可以是一个成熟的类

例如,我希望在CI中使用本机电子邮件类,但我认为我将在各种不同的控制器中使用它,并希望它尽可能“干燥”。将代码抽象为向助手或库发送电子邮件是否有意义?或者我应该在需要的控制器中重复它吗?我还有一个基本控制器。也许我应该把代码放在那里?它的使用频率足以重复,但并不是每个控制器都在使用它

根据文档,我希望抽象的重复代码类似于以下内容:

下面是我的代码

$this->load->library('email');

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

我将使用电子邮件模板,并将
$data
数组传递给模板。

我建议使用
助手
方法对您非常有用。只需在
autoload.php
中添加一个
custom
助手,如下所示:

$autoload['helper'] = array('custom'); 
并添加一个
send\u email()
方法,如下所示

function send_email() 
{
    $ci = & get_instance();
    $ci->load->library('email');

    $ci->email->from('your@example.com', 'Your Name');
    $ci->email->to('someone@example.com');
    $ci->email->cc('another@another-example.com');
    $ci->email->bcc('them@their-example.com');

    $ci->email->subject('Email Test');
    $ci->email->message('Testing the email class.');

    $ci->email->send();
}

更多信息:

我建议您使用
助手
方法。只需在
autoload.php
中添加一个
custom
助手,如下所示:

$autoload['helper'] = array('custom'); 
并添加一个
send\u email()
方法,如下所示

function send_email() 
{
    $ci = & get_instance();
    $ci->load->library('email');

    $ci->email->from('your@example.com', 'Your Name');
    $ci->email->to('someone@example.com');
    $ci->email->cc('another@another-example.com');
    $ci->email->bcc('them@their-example.com');

    $ci->email->subject('Email Test');
    $ci->email->message('Testing the email class.');

    $ci->email->send();
}

更多信息:

助手方法最适合于此,因为它不使用控制器,并且可以在控制器和视图中使用,无需控制器或任何其他库的帮助。好吧,我想我会选择助手。谢谢一个助手方法最适合这样做,因为它不使用控制器,并且可以在控制器和视图中使用,而无需控制器或任何其他库的帮助。好吧,我想我会选择一个助手。谢谢