1
\$\begingroup\$

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(
        array(
            'field' => 'name',
            'label' => 'Series name',
            'rules' => 'trim|required'
        ),
        array(
            'field' => 'start',
            'label' => 'Start',
            'rules' => 'trim|required|valid_date'
        ),
        array(
            'field' => 'end',
            'label' => 'End',
            'rules' => 'trim|required|valid_date'
        ),
        array(
            'field' => 'pursuit',
            'label' => 'Pursuit'
        ),
        array(
            'field' => 'userId',
            'label' => 'userId',
            'rules' => 'required|integer'
        )

    );
    $this->form_validation->set_rules($rules);
    $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)

            $this->load->helper('form');

            /** @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);
            $this->load->view('footer');

        } 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');
            }
            redirect('series');
        }
    }

}
\$\endgroup\$
  • \$\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.