I feel although my controller is over complicated and more should be moved to the model.

I've read that the controller shouldn't contain any logic, and just call the model/view, however it currently contains logic which I don't know how to move to the model. Am I correct in thinking that this should be moved or is it's current state acceptable.

It's worth noting that I'm only unit-testing and reporting on models for the reasons described above.

public function create()
    $this->load->library(array('form_validation', 'My_form_validation', 'session'));

    $rules = array(
            'field' => 'name',
            'label' => 'Series name',
            'rules' => 'trim|required'
            'field' => 'start',
            'label' => 'Start',
            'rules' => 'trim|required|valid_date'
            'field' => 'end',
            'label' => 'End',
            'rules' => 'trim|required|valid_date'
            'field' => 'pursuit',
            'label' => 'Pursuit'
            'field' => 'userId',
            'label' => 'userId',
            'rules' => 'required|integer'

    $this->form_validation->set_message('valid_date', 'The {field} field must be in the format yyyy-mm-dd');

    $group = array('admin', 'seriesManage');

    if ($this->_is_authorized($group)) { //The user is authenticated and authorized

        if ($this->form_validation->run() == FALSE) { //form not successfully submitted (or not at all)


            /** @noinspection PhpUndefinedFieldInspection */
            $data['site_name'] = $this->config->item('site_name');
            //get current user id
            $data['userId'] = $this->ion_auth->user()->row()->id;

            $this->load->view('header', $data);
            $this->load->view('series/create', $data);

        } else { //form successfully submitted and validated, time to save the response!
            if ($this->series->create()) {
                $this->session->set_flashdata('message', 'New series created successfully!');
            } else {
                $this->session->set_flashdata('message', 'Failed to save to database, please contact system administrator');

  • \$\begingroup\$ Welcome to code review. Suggestion for the title, "MVC Controller using Code Ignighter". Can you tell us a little more about the controller and the model? It's not quite clear what you are asking. \$\endgroup\$ – pacmaninbw Sep 21 '16 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.