【发布时间】:2013-04-04 08:00:24
【问题描述】:
我必须使用 Codeigniter 在我的应用程序中做出一些设计决定。
我在控制器中有一个方法,它调用一个库来创建 PDF。 另外我有一些类将数字作为参数并返回字符串(口头数字)。
我想知道在所有此类之间传递数据的最佳做法是什么。这是控制器调用所有库(在步骤 2 和步骤 3 之间)并将所有准备好的数据提供给将创建 PDF 的模型的任务吗?或者这是模型本身的任务,通过加载和调用将数字转换为字符串的类来转换提供的原始数据。
就松散耦合以及代码的模块化和清晰度而言,最佳解决方案是什么。
这是一个控制器:
class Payu extends CI_Controller
{
public function raport($task_id)
{
/* (step 1) Load necessarty models */
$this->load->model('MTasks');
$this->load->model('mpdfinvoice');
/* (step 2) task details from DB */
$task_details = $this->MTasks->getTaskDetails($task_id);
/* (step 3) create PDF that will be send */
$this->mpdfinvoice->pdf($task_details);
/* (step 4) compose an email with attached pdf */
$this->email->from($this->config->item('noreply_email'));
$this->email->to($task_details['email']);
$this->email->attach('invoiceJustCreated.pdf');
$this->email->subject('opłaciłes to zlecenie');
$message = 'some message goes here';
$this->email->message($message);
$this->email->send();
}
}
This is a model that creates PDF file (called by controller)
class mpdfinvoice extends CI_Model
{
public function pdf($task_details)
{
/* (step 1) load necesary library and helper */
$this->load->library(array('fpdf' ));
$this->load->helper('file');
/* (step 2) set PDF page configuration*/
$this->fpdf->AddPage();
$this->fpdf->AddFont('arialpl','','arialpl.php');
$this->fpdf->SetFont('arialpl','',16);
/* (step 3) show data on PDF page */
$this->fpdf->cell('','',$task_details['payment_amount'] ,1);
/* I want to have "payment amount" verbally here
So Should I load and call the convert class here or
should I have this data already prepared by the controller
and only output it ? */
}
}
【问题讨论】:
-
在 MVC 中,控制器只负责将适当的数据从用户请求传递到模型层。没有其他的。它不应该发送消息、渲染模板或类似的东西。底线是:CodeIgniter 没有实现 MVC 或 MVC 启发的设计模式。
-
那么模型应该发送电子邮件吗? CodeIgniter 没有实现 MVC 是什么意思?
-
模型不是类或对象。它是一层。你可以阅读更长的解释here。至于 CodeIgniter:它没有视图。只有简单、愚蠢的模板。这会强制“控制器”中的 UI 逻辑,破坏表示层的 SoC。而且在大多数实现中,没有模型层。仅基于活动记录的实体的集合。这反过来又强制控制器中的应用程序逻辑,打破模型层和表示层之间的 SoC。
-
您认为哪个框架最接近正确的 MVC 实现?
-
框架不实现 MVC。他们不应该声称他们这样做了。框架应该提供工具,让用户选择合适的架构。在 PHP-verse 中,符合此描述的将是 Zend Framework 2.x 和 Symfony 2.x .. 此外,可能值得尝试新的 Laravel,它已经摆脱了 rails-clone 的心态。再说一次,如果你想制作一个基于 MVC 的应用程序,就没有必要使用框架。从框架中学习 MVC 就像从 Wordpress 中学习良好的编程实践一样。
标签: php codeigniter model-view-controller fpdf loose-coupling