Receive POST data in a controller or model

I am learning CodeIgniter. I am wondering which one is the best practice for receiving POST form data when updating the database: from the controller or from the model?

If we get the POST data in the controller, we need to pass the data as arguments to the Model function, but the Model function can be used in other forms. If we get the POST data directly in the Model, then we can exclude the step of passing arguments, but it is limited to some specific forms.

Which one is the best?

Thanks.

+4
source share
2 answers

It would be best practice to do all the data processing in the controller and service level, and then only transfer the necessary data to either Modal or View. Thus, you have a controlled flow of information and a clear separation of concepts. Model objects should only be used as data objects through ORM, and nothing more. The controller must contain all the business logic.

+3
source

It is best to process the code of your kind (form responses, etc.) in your controllers, and then pass everything you need as parameters for your models.

This allows you to reuse your model (and its methods) most often if, for example, you have a different view and controller that requires this model to process forms differently (for example, the API).

+5
source

Source: https://habr.com/ru/post/1380079/


All Articles