MVC PHP - Sending mail from Model

倖福魔咒の 提交于 2020-01-03 10:19:19

问题


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 ?


回答1:


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




回答2:


You should be sending mail from the controller, reading data / etc from the model when / if required.




回答3:


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...



来源:https://stackoverflow.com/questions/5017369/mvc-php-sending-mail-from-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!