English 中文(简体)
Is using a Mail Model in MVC incorrect?
原标题:
  • 时间:2009-11-18 05:26:19
  •  标签:
  • php
  • kohana

I have built a model in some of my MVC websites to assist with sending emails, generally I do something like this

$mail = new Mail_Model;
$mail->to( me@somewhere.com );
$mail->from( you@somewhere.com );
$mail->subject( hello );
$mail->body( hello how are you );
$mail->send();

I know the model is meant to model data - so is what I m doing a violation of that? Should it be a helper class instead? Something like...

Mail::send( me@somewhere.com ,  you@somewhere.com ,  hello ,  hello how are you );

That second one doesn t read as well to me.. of course I could pass in an array with named keys to make it more readable.

So is my Mail model violating what a model should be in the MVC paradigm?

最佳回答

since this question is tagged with kohana maybe you already know that kohana has an email helper that does exactly what you re doing here.

inherently, my answer is that the code to send an email would fit better as a helper.

问题回答

The mail isn t really a model, I suspect, unless you re saving it to the database or doing some other complicated processing with it.

IMO, instantiating an object in this case is probably not very MVC , and your second example looks (at least to me) more like a line of code that indicates a one-off action with no real business logic repercussions.

But - don t feel too constrained by the paradigm! Which of those two options do you think is easier to read, and easier for another coder to follow along with later?

Take _Model off your first one and that would make perfect sense to me. Implementing mail-sending as a single static function gives you no benefit over just calling mail(), really. you ll end up with the same problems trying to set additional headers, or send HTML email, or add attachments that extra methods on a class can abstract from your calling code.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签