MVC PHP - 从模型发送邮件 [英] MVC PHP - Sending mail from Model

查看:79
本文介绍了MVC PHP - 从模型发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我从模型或控制器发送邮件时,我有问题。在控制器中我使用像

I am having problem to figure whenever I should send mail from the Model or the Controller. The thing is In the controller i use like

这是关于PHP。

>

In Controller:

if (Post::get()){
   $this->model->registerUser( ... );
   $this->model->mailSendUserActivation();
   // assign something to view.
}

在模型中:

public function mailSendUserActivation(){
    $mail = new \com\Mail();
    // assign stuff to mail from API classes and other functions in model.
    $mail->send();
}

这是否正确?或者邮件确实是从控制器发送的。

Is this correct ? Or should the mail really be sent from the controller ?

推荐答案



控制器应处理与用户的交互。

发送邮件是一项操作,因此您应在控制器中处理。

如果发送电子邮件需要复杂的代码(说多于几行)考虑将其提取到一些帮助类,以保持您的控制器苗条和凝聚力。因此,我将在一些辅助类方法中发送电子邮件的代码,并在控制器操作中调用它。

Model should describe you domain model.
Controller should handle interaction with user.
Sending mail is an action so you should handle it in controller.
If sending email requires complicated code (say more than few lines) consider to extract it to some helper class to keep your controller slim and cohesive. So I would put code for sending email in some helper class method and just call it in controller action.

维基百科上的MVC

这篇关于MVC PHP - 从模型发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆