MVC PHP - 从模型发送邮件
我无法确定何时应该从模型或控制器发送邮件。事情是在我使用的控制器中,就像
这是关于 PHP 的。
在控制器中:
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();
}
这是正确的吗?或者邮件真的应该从控制器发送吗?
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
This is regarding PHP.
In Controller:
if (Post::get()){
$this->model->registerUser( ... );
$this->model->mailSendUserActivation();
// assign something to view.
}
In Model:
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
模型应该描述您的域模型。
控制器应该处理与用户的交互。
发送邮件是一个操作,因此您应该在控制器中处理它。
如果发送电子邮件需要复杂的代码(比如多于几行),请考虑将其提取到某个辅助类,以保持控制器的简洁和内聚性。因此,我会将用于发送电子邮件的代码放入一些辅助类方法中,然后在控制器操作中调用它。
维基百科上MVC 的良好解释
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.
Good explanation of MVC on wikipedia
您应该从控制器发送邮件,在需要时/从模型中读取数据/等。
You should be sending mail from the controller, reading data / etc from the model when / if required.
我对将应用程序逻辑和工作流程放入控制器本身感到非常困惑。发送业务电子邮件应该计入应用程序逻辑和工作流程,并且根据 MVC 架构,模型应该保存业务的应用程序逻辑和工作流程,因为它是唯一的就控制器而言,它了解所有业务工作流程和逻辑,我很困惑,是否应该告知控制器业务流程实现,或者是否应该直接联系在您的情况下实现邮件服务的帮助程序类..但是,我看到,如果控制器从模型中获取消息、主题和消息的其他属性的组成,然后将它们传递给邮件程序帮助类,那么这里更有意义..因为模型仍然知道细节消息和控制器角色的作用可以从模型中获取消息详细信息并将其传递给帮助器类。在这种情况下我很困惑,如果通过帮助程序类发送电子邮件时发生错误,是否应该通知模型,或者控制器将其记录到文件或将其呈现给视图。但是,考虑联系服务提供商模型直接确实有意义。在这种情况下,控制器仅向模型发送请求,模型一方可以代表其联系服务提供商以完成其工作并将输出传递回控制器,控制器可以进一步将其传递以进一步查看或处理...
I m pretty confused with putting application logic and workflows in the controller itself.. Sending business emails should be counted in the application logic and workflow and according to MVC architecture, A model should hold the application logic and workflow of business as It is the only object which is aware of all the business workflows and logics as far as controller concern, I am confused, if the controller should be told about the business flows implementation and or should it contact directly to a helper class which implements the mailing services in ur case.. However, I see, if Controller gets the composition of messages ,subject, and other properties of message from the model and then it passes them to the mailer helping class, that makes more sense here.. as Model still knows about the details of the message and controller role could be just made to get the message detail from the model and pass it to the helper class. i am confused in this case,if an error occurs while sending email via helper class, the model should be informed or not , or controller either logs it to a file or presents it to View.. however, Thinking of contacting a Service provider From the Models directly does make sense. in that case, Controller only sends requests to the model, Model on its side could contact service provider on its behalf to get its work done and pass the output to the controller back which further could pass it to view or process further...