Where should $this->input->post be in model or controller? |
[eluser]cleansugar[/eluser]
option 1: model: Code: function simpan() { option 2: controller: Code: function submit(){ model: Code: function inserData() { If I want to pass POST data to DB, at option1, $this->input->post is in model and at option 2, $this->input->post is in controller. Which is right in MVC pattern?
[eluser]naren_nag[/eluser]
Classic MVC pattern -- variable names in the model map to fields in the table where we validate Model: Code: class Product extends Model { Controller -- we get our values from our view, validate them, and then call our model Code: .. snip .. Also, there is no one way -- take a look at CI's inbuilt form validation class. That's a pretty good pattern to follow. cheers, naren
[eluser]Rick Jolly[/eluser]
Option #2. Generally, you want to decrease hard-coded dependencies within your code. Say, for example, you decided to allow model variables to be set using input other than $_POST. With option #1, you'd have to change your model class. Normally, where possible, we should pass dependencies into our classes. This is known as Dependency Injection. |
Welcome Guest, Not a member yet? Register Sign In |