简体   繁体   English

Codeigniter应用程序:避免在同一控制器的多种方法中重复代码

[英]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.8Bootstrap 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.

相关问题 在 PHP 中有没有一种方法可以避免在一个类的多个方法中重复相同的 try/catch 代码? - In PHP is there a way I can avoid repeating the same try / catch code in multiple methods of a class? CodeIgniter在同一控制器类上有多个子功能或方法? - CodeIgniter Multiple Sub-Functions or Methods on Same Controller Class? Codeigniter:如何在每个方法(或查看页面)中显示登录的用户名,而无需在每个函数中重复相同的代码 - Codeigniter : How to show logged In User Name in every methods (or view page) without repeating same code in every function 避免在 codeigniter 3 控制器中重复会话和访问权限检查 - Avoid repeating session and access right checks in codeigniter 3 controller 在这种情况下如何避免重复相同的代码 - How to avoid repeating the same code in this case CodeIgniter在同一个控制器中调用方法 - CodeIgniter Calling methods within the same controller Codeigniter会话不适用于同一控制器的其他方法 - codeigniter session is not working on other methods of the same controller Codeigniter 3:我怎样才能避免在控制器中重复这段代码? - Codeigniter 3: how can I avoid repeating this chunk of code in my controllers? 控制器在 CodeIgniter 中的 URL 中重复 - Controller repeating in URL in CodeIgniter 如何组合多个方法而不在Controller中重复查询? - How to combine multiple methods without repeating queries in Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM