Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

}
share|improve this question
    
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. – pacmaninbw Sep 21 '16 at 17:07
    
thanks @p I've update the question. – Roshan Bhumbra Sep 21 '16 at 17:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.