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
?
One way to make a POST is to use HTML. BeginForm() and pass Controller and Action names along with FormMethod. POST to BeginForm(). Inside the BeginForm, you can have a HTML Input Button of type Submit to make the POST call to Controller action.
Both GET and POST method is used to transfer data from client to server in HTTP protocol but the Main difference between the POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from ...
An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic.
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 the
Request::getParam()
method with something likeRequest::getPost()
- although I have come to the conclusion that, in a correctly structured application, theGET
andPOST
parameters 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.
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.
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.
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:
Request
instance, that controller receivesThis 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.
Once upon a time was the three tiered application architecture.
It all depends on your MVC framework. Normally, the Controller does the link between the user and the model layer, which manipulate domain objects.
In the early days of MVC in PHP, the model layer was actually just the domain objects, called models for that purpose. Some prefered having so called thin models, which only provide an OO representation of the data, (which simplifies persistence). In that case, the controller would regroup the so called actions, containing the bulk of the processing associated with an HTTP request (fat controller).
Others embedded most of said processing in the object model with dedicated methods (fat model).
However, at some point, you have to analyse the content of the query to sanitize and validate it, and this depends on how your view will format the request. Sanitization might be a controller task (this request should only contain these values), while validation is definitely a model task (values should be of these types).
An interesting question is: how do you deal with actions impacting several domain objects? Where do you put the logic for that?
Nowadays, the model layer is composed of services segregating the domain objects from the evil grasp of the controllers, to limit the dependencies between the layers to their respective interfaces only. This is where most of the request processing is done.
Symfony2, for instance, provides a sensible answer to this question: each step of the processing of a request is implemented in a dedicated piece of code, which could be described as the following:
The service job is then broken in several steps:
CakePHP is another popular framework which follows similar concepts: simple controllers, and services encapsulating domain objects.
See this question for a better insight on the general concepts.
See this other question for other answers.
Thanks to tereško for his invaluable input on the matter.
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