0

I have a “simple” question about the principle from the CodeIgniter MVC. If I take a look in the manual from CI (Models) I see for example this:

function insert_entry()
{
    $this->title   = $_POST['title']; // please read the below note
    $this->content = $_POST['content'];
    $this->date    = time();
    $this->db->insert('entries', $this);
}

Well, ok – to put in data this way is bad I know :) but also if we user “$this->input->post()” … for me it doesn’t look better. Isn’t it better to handle the data in the controller before I use a function from a model? Maybe the model part looks so:

function insert_entry($data)
{
    $this->db->insert('entries', $data);
}

And in the controller such like this:

$this->load->model('Blog');
$data = array();
$data['title'] = $this->input->post('title');
$data['content'] = $this->input->post('content');
$this->Blog->insert_entry($data);

But where i run the validation etc. … model or controller? Maybe someone understand what I would like to know. Maybe you have some more experience, links or whatever. Thanks!

spotlr
  • 319
  • 5
  • 15

2 Answers2

6

If you are trying to implement proper MVC or MVC-inspired design pattern with CodeIgniter, you have already failed. CodeIgniter does not follow the ideas of MVC and related patterns. It actually just clones the pattern used in Rails (I can elaborate in comments section, if you want to know why and how).

That said ...

The reason why $this->input->post() is used in controllers is to provide some abstraction and separate your code from PHP's superglobals. What you call a "controller" should collect data from the user's request and pass it to the model layer’s structures. The model layer should be completely unaware of the front-end. The domain business logic for creating an invoice does not change just because you renamed the <input/> for invoice number from "innr" to "number".

The data validation should happen in the model layer. When done properly, the code for validation is part of domain objects and data integrity checks would be handled by storage abstraction (for example, a data mapper), but in CodeIgniter people usually lump both domain and storage logic together and call it: "models". Of course that violated SRP, but CI users don't care and are even unaware of such principles. So basically, when writing for CI, the validation should happen in "models".

If you want to read more about the whole subject, you might find this post relevant.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • this is what i mean, thanks ... "I can elaborate in comments section, if you want to know why and how" please tell me more ;) – spotlr Sep 28 '12 at 07:01
  • 1
    Rails framework was create in '05 and it was mean to be prototyping framework (for fast building of throw-away code) .. at which it is quite good. But it was never meant to be used for large, production-stage applications. Instead of implementing full MVC, it opted for list of ActiveRecord instance to act as "model" and merged view and controller as single entity, called "controller". That left it with no view, therefore they decided to refer to templates as "views". This was extremely good solution **for prototyping**. So good that every other framework started to copy it and calls "MVC". – tereško Sep 28 '12 at 13:04
  • @kailange , also , as side-effect of using ActiveRecords, some of the domain business logic tends to be forced in "controllers". Which is because AR are directly associated with tables in database and you end up with situation, where you have nowhere you put logic for interaction with two unrelated AR instances. This causes controllers to grow. Code becomes unmaintainable and confusing. Trick is - it was never intended to be maintained. – tereško Sep 28 '12 at 13:07
  • well, i will take a look later - maybe you have some links to read? – spotlr Sep 28 '12 at 14:52
0

hi you would have something like

class new_controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function insert_db_entry() {
        $this->load->model('Blog');
        $data = array();
        if($this->input->post("submit")) {

            $this->load->library("form_validation");

            //create the form validation rules

            if($this->form_validation->run() === TRUE) {

                $data['title'] = $this->input->post('title');
                $data['content'] = $this->input->post('content');
                $this->Blog->insert_entry($data);
            }
            else {
                $errors = validation_errors();
            }
        }
    }

}

you use the form validation library to handle the validation when the form submit is detected.

mic
  • 1,251
  • 2
  • 15
  • 33
  • http://codeigniter.com/user_guide/libraries/form_validation.html This is the CI instruction manual use this for most things you need to know about using this framework – mic Sep 27 '12 at 21:03
  • thanks first but this is not directly what i mean ;9 i would like to know wahat is the best "way" to write code with the mvc ... in your code the "$this->Blog->insert_entry($data);" runs the model but the Next question would be where to check if the insert (in this example) was successfully ?! ... – spotlr Sep 27 '12 at 21:06
  • ok, the models i have always used for handling database queries and just returning the data. controllers i use for most things including form submissions and validations. Helpers can be very useful if u have functions that you want to have access to over several different controllers. – mic Sep 27 '12 at 21:12
  • i have edit your post, so you think this way would be "good" ? also avaible here: http://pastebin.com/wVgGUFLw – spotlr Sep 27 '12 at 21:31
  • yer that looks fine you just need to define some rules for the form_validation to work correctly, and it would be good to go. $this->form_validation->set_rules('title', 'Title', 'required|min_length[5]|max_length[12]'); you would insert that between loading the form_validation library and the if. you create one for each form element you are going to be submitting. – mic Sep 27 '12 at 21:59