[英]Codeigniter application: avoid repeating code in multiple methods of the same controller
I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4 . 我正在使用Codeigniter 3.1.8和Bootstrap 4开发一个基本的博客应用程序。
In my Posts controller I have three methods that repeat 7 lines of code instead of sharing them: 在我的Posts控制器中,我有三种方法可以重复7行代码而不是共享它们:
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 . 我认为创建扩展CI_Controller的基本控制器然后使我的所有控制器扩展我的基本控制器是“有效的”,因为该基本控制器仅与post有关 。
What is the best alternative to creatibg a base controller in this case? 在这种情况下,创建基础控制器的最佳替代方法是什么?
Easy to do. 容易做。 Simply create a "worker" method in the controller that runs the repeating code and that returns the $data
array. 只需在控制器中创建一个“工人”方法,该方法运行重复代码并返回$data
数组。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.