我在想什么时候应该从模型或 Controller 发送邮件时遇到问题。问题是在我使用的 Controller 中
这是关于 PHP 的。
在 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();
}
这是正确的吗?或者邮件真的应该从 Controller 发送?
最佳答案
模型 应该描述您的域模型。
Controller 应处理与用户的交互。
发送邮件是一个 Action ,所以你应该在 Controller 中处理它。
如果发送电子邮件需要复杂的代码(比如不止几行),请考虑将其提取到某个辅助类中,以保持 Controller 的简洁和内聚。所以我会把发送电子邮件的代码放在一些辅助类方法中,然后在 Controller 操作中调用它。
很好的解释MVC on wikipedia
关于php - MVC PHP - 从模型发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5017369/