CodeIgniter and the Model-View-Controller – your experience / your meaning?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 01:57:41
tereško

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!