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