dongwei4652 2013-12-24 14:31
浏览 16
已采纳

谁应该抓住我的$ _POST数据:控制器或型号? [重复]

This question already has an answer here:

My question

I'm unsure about the handling of $_POST data in a MVC architecture: Who should catch the $_POST data: The controller or the model ? Several sources say "skinny controllers, fat models", but same sources also say that a model should be strictly decoupled from the application (and example B clearly shows a "fat" model, but is not decoupled as it directly asks for POST data). For comparison let's see the same thing written in two different ways (example pseudo-code):

A.) The controller grabs $_POST values, passes it as arguments to model

// CONTROLLER
public function createSomething()
{
    $model = new Model;
    $model->createThis($_POST['stuff_from_form']);
}

// MODEL (expects argument)
public function createThis($stuff)
{
    // and here the model method does whatever it does
}

B.) The model grabs $_POST values

// CONTROLLER
public function createSomething()
{
    $model = new Model;
    $model->createThis();
}

// MODEL (expects NO argument, grabs POST data directly)
public function createThis()
{
    $stuff = $_POST['stuff_from_form'];
    // and here the model method does whatever it does
}
</div>
  • 写回答

2条回答 默认 最新

  • doujiyan0971 2013-12-25 00:04
    关注

    Your controller should manage all validation and only pass verified / clean data to your models. The reason for this is that you want to keep your models generic enough for re-use and all access to the models/database should be validated / cleansed before hand.

    Also don't use $_POST directly if you are using something like CodeIgniter then use their handler $input->post->('name'); (or similarly for any PHP framework).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?