0

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.

In my Posts controller I have three methods that repeat 7 lines of code instead of sharing them:

public function index() {

    // More code here

    /* This code is in all three methods */
    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['number_of_pages'] = $this->Pages_model->count_pages();
    $data['number_of_posts'] = $this->Posts_model->get_num_rows();
    $data['number_of_categories'] = $this->Categories_model->get_num_rows();
    $data['number_of_comments'] = $this->Comments_model->get_num_rows();
    /* This code is in all three methods */
    $data['posts'] = $this->Posts_model->get_posts($limit, $offset);
    $data['offset'] = $offset;

    $this->load->view('partials/header', $data);
    $this->load->view('dashboard/posts');
    $this->load->view('partials/footer');
}

public function create() {

    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['number_of_pages'] = $this->Pages_model->count_pages();
    $data['number_of_posts'] = $this->Posts_model->get_num_rows();
    $data['number_of_categories'] = $this->Categories_model->get_num_rows();
    $data['number_of_comments'] = $this->Comments_model->get_num_rows();
    $data['tagline'] = "Add New Post";

    // More code here
}

public function edit($id) {
    // More code here

    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['number_of_pages'] = $this->Pages_model->count_pages();
    $data['number_of_posts'] = $this->Posts_model->get_num_rows();
    $data['number_of_categories'] = $this->Categories_model->get_num_rows();
    $data['number_of_comments'] = $this->Comments_model->get_num_rows();
    $data['post'] = $this->Posts_model->get_post($id);
}

I do not believe it is "effective" to create a base controller that extend the CI_Controller and then make all my controllers extend my base controller, as this base controller is related only to the posts.

What is the best alternative to creatibg a base controller in this case?

1
  • 3
    Create a method that populates data with your common attributes? Commented Aug 14, 2019 at 21:09

1 Answer 1

1

Easy to do. Simply create a "worker" method in the controller that runs the repeating code and that returns the $data array.

public function index()
{
    // More code here
    $data = $this->get_data();
    $data['posts'] = $this->Posts_model->get_posts($limit, $offset);
    $data['offset'] = $offset;

    $this->load->view('partials/header', $data);
    $this->load->view('dashboard/posts');
    $this->load->view('partials/footer');
}

public function create()
{
    $data = $this->get_data();
    $data['tagline'] = "Add New Post";

    // More code here
}

public function edit($id)
{
    // More code here

    $data = $this->get_data();
    $data['post'] = $this->Posts_model->get_post($id);
}

// Here's your worker method. 
// Note it is "private" so it can only be called from within this controller
private function get_data()
{
    /* This code is in all three methods */
    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['number_of_pages'] = $this->Pages_model->count_pages();
    $data['number_of_posts'] = $this->Posts_model->get_num_rows();
    $data['number_of_categories'] = $this->Categories_model->get_num_rows();
    $data['number_of_comments'] = $this->Comments_model->get_num_rows();
    return $data;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Whi is the worker method the kast of them? Wouldn't it be better if it were the first?
The order of methods in the class has no effect on the performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.