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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With