在 MVC 中处理 $_POST 数据的正确方法是什么? [英] What is the right way to handle $_POST data in MVC?

查看:28
本文介绍了在 MVC 中处理 $_POST 数据的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 PHP 系统中有一个常见的 MVC 情况:Controller 从包含 $_POST 数据的 View 接收请求.现在我有三种处理数据的方法:

a) Controller 只调用 ModelModel 处理 $_POST 数据.
b) Controller$_POST 数据转换成变量并传递给 Model.
c) Controller$_POST 数据转换成Model 的域对象,并且只将对象传递给Model.

目前,我遵循选项A,但我认为这是错误的,所以我正在考虑使用选项C.

那么,根据 MVC,处理 $_POST 数据的正确方法是什么?

编辑目前,我没有使用任何 MVC 框架.

EDIT 2 通常,同一个 Controller 处理来自浏览器、Web 服务、离线应用程序等的请求,或者每个都有自己的 Controller?

解决方案

最好的选择是使用 #2 方法,并进行一些改动.
我会把它写成这样:

公共函数 postLogin( $request ){$service = $this->serviceFactory->build('Recognition');$service->authenticate( $request->getParam('username'),$request->getParam('password'));}//是的,这就是整个方法

如果您使用了诸如 Request 实例之类的东西来抽象用户的输入,则无需实际创建变量.

<块引用>

另外,你可能想用Request::getPost()之类的东西替换Request::getParam()方法——尽管我已经来了结论是,在结构正确的应用程序中,GETPOST 参数不应共享相同的名称.

您在代码片段中看到的 serviceFactory 将是您在控制器和视图实例中注入的对象.它可以让您在控制器和视图之间共享相同的服务实例.

它负责创建服务(其中将包含应用程序逻辑,而将域业务逻辑留在域对象)中,其中帮助您将域实体和存储抽象之间的交互与表示层隔离.

关于其他选项:

  • <块引用>

    Controller 只调用 Model,Model 处理 $_POST 数据.

    在 MVC 和受 MVC 启发的设计模式中,模型既不应该了解用户界面,也不应该了解整个表示层.PHP 中的 $_POST 变量是一个 superglobal.

    如果您将其与模型层一起使用,您的代码将绑定到 Web 界面甚至特定的请求方法.

  • <块引用>

    Controller 将 $_POST 数据转化为 Model 的对象,并且只将对象传递给 Model

    不完全确定您的意思.似乎您在谈论抽象的实例化,它将包含用户的请求.但在这种情况下,控制器负责所述结构的实例化/创建,这将违反 SRP.

结束语:

您必须了解的一件事是,在基于 Web 的 MVC 应用程序的上下文中,您的应用程序的用户是浏览器.不是你.浏览器发送请求,由路由机制处理并由控制器传播.视图产生响应到您的浏览器.

另一件事是:模型既不是类也不是对象.模型是一个层.


更新

<块引用>

通常,同一个控制器处理来自浏览器、Web 服务、离线应用程序等的请求,或者每个控制器都有自己的控制器?

您应该能够拥有一个处理所有形式的应用程序的控制器.但这只是在条件下,您实际上对所有 3 个用例都使用相同的应用程序.

这样做有两个条件:

  • 您需要抽象控制器接收的 Request 实例
  • 视图应该在控制器之外实例化

这样您就可以拥有一个应用程序来满足所有要求.唯一不同的是,每个变体都有不同的地方,即引导阶段,您可以在此创建 Request 实例并选择正确的视图.

在您描述的情况下,更改部分实际上是视图,因为 REST 或 SOAP 服务预计会产生与普通 Web 应用程序不同的响应.

I have the common MVC situation in my PHP system: the Controller receive a request from the View containing $_POST data. Now I have three ways to handle the data:

a) The Controller only calls the Model and the Model handle the $_POST data.
b) The Controller transforms the $_POST data into variables and pass them to Model.
c) The Controller transforms $_POST data into a Model's domain object and only pass the object to Model.

Currently, I am following option A, but I believe it is wrong, so I am thinking of using option C.

So, according to MVC, what is the right way to handle $_POST data?

EDIT At the moment, I'm not using any MVC framework.

EDIT 2 Generally, the same Controller handles request from a browser, a web service, an offline application, etc, or each one has it own Controller?

解决方案

The best option is to use #2 approach, with some alterations.
I would write it as something like this:

public function postLogin( $request )
{
     $service = $this->serviceFactory->build('Recognition');
     $service->authenticate( $request->getParam('username'),
                             $request->getParam('password') );
}
// Yes, that's the whole method

There is no need to actually create variables, if you have used something like a Request instance to abstract the user's input.

Also, you might want to replace theRequest::getParam()method with something likeRequest::getPost()- although I have come to the conclusion that, in a correctly structured application, theGETandPOSTparameters should not share same name.

The serviceFactory which you see in the code snippet would be an object that you inject in both controller and view instance. It would let you share same service instances between controllers and views.

It is responsible for creation of services (which would contain the application logic, while leaving the domain business logic in the domain objects), which helps you isolate the interaction between domain entities and storage abstractions from the presentation layer.

About the other options:

  • The Controller only calls the Model and the Model handle the $_POST data.

    In the MVC and MVC-inspired design patterns the model should be aware of neither the user interface nor of the presentation layer as whole. The $_POST variable in PHP is a superglobal.

    If you use it with model layer, your code becomes bound to the web interface and even the specific request method.

  • The Controller transforms $_POST data into a Model's object and only pass the object to Model

    Not entirely sure what you meant with this. Seems you were talking about instantiation of an abstraction, which would contain the user's request. But in this case controller becomes responsible for instantiation/creation of said structure, which would violate SRP.

Closing notes:

One thing you must understand is that, in context of web based MVC applications, the User of your application is the browser. Not you. Browser sends the request, which is handled by routing mechanism and disseminated by controller. And view produces the response to your browser.

And the other thing is: Model is neither a class nor an object. Model is a layer.


Update

Generally, the same Controller handles request from a browser, a web service, an offline application, etc, or each one has it own Controller?

You should be able to have single controller, that deals with all the forms of application. But that is only on the condition, you are actually using same application for all 3 use-cases.

To do so there are two conditions:

  • you need to abstract the Request instance, that controller receives
  • the view should be instantiated outside the controller

This way you can have one application to fulfill all the requirements. Only thing, that each variant has different, is the bootstrap stage, where you create the Request instance and select the proper view.

In the situation, that you described, the changing part would actually be the view, since a REST or SOAP service would be expected to produce a different response than an ordinary web application.

这篇关于在 MVC 中处理 $_POST 数据的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆