Jason
Jason

Reputation: 119

Get POST data in controller or model

I am learning CodeIgniter. I am wondering which one is the best practice to get the form POST data when updating the database: from Controller or from Model?

If we get the POST data in the Controller, then 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 eliminate the argument passing step, but it's limited to some specific forms.

Which one is the best practice?

Thanks.

Upvotes: 4

Views: 2176

Answers (2)

Zaki Saadeh
Zaki Saadeh

Reputation: 652

The best practice would be to do all data processing in the Controller and the service layer, and then pass only the required data to either the Modal or the View. That way, you have a controlled flow of information and a clean separation between the concepts. The Model objects should only be used as data objects via ORM, and nothing more. The controller should contain all business logic.

Upvotes: 4

Emma
Emma

Reputation: 6572

It is best practice to handle your view code (form responses, etc) in your controllers, and then pass anything you need as parameters to your models.

This allows for the most reuse, as your model (and it's methods) can be reused, if, for example, you have a different view and controller requiring that model which processes forms in different ways (e.g. an API).

Upvotes: 5

Related Questions