You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter 3博客应用:如何避免冗余的分页$config数组?

Clean Solutions for Reusable Pagination Config in CodeIgniter 3.1.8

Hey there! Duplicating pagination config across controllers is definitely a code smell—let's fix that with a few straightforward, maintainable approaches. Here are my top recommendations:

1. Use a Custom Config File

This is the simplest approach if you just want to centralize your base pagination settings.

Step 1: Create a Pagination Config File

Create application/config/pagination.php and define all your shared config parameters here:

<?php
$config['per_page'] = 10;
$config['full_tag_open'] = '<nav class="pagination">';
$config['full_tag_close'] = '</nav>';
$config['first_link'] = '« First';
$config['last_link'] = 'Last »';
$config['next_link'] = 'Next →';
$config['prev_link'] = '← Previous';
// Add any other shared config options here

Step 2: Load & Override in Controllers

In your Posts.php or Categories.php controller, load the config, then override only the unique parameters:

// In Posts Controller
public function index() {
    // Load the base pagination config
    $this->load->config('pagination');
    $pagination_config = $this->config->item('pagination');

    // Override the unique values
    $pagination_config['base_url'] = site_url('posts/index');
    $pagination_config['total_rows'] = $this->post_model->get_total_post_count(); // Replace with your model call

    // Initialize pagination
    $this->pagination->initialize($pagination_config);

    // Rest of your controller logic...
}

Do the same in your Categories controller—just swap out base_url and total_rows with category-specific values.

2. Extend the Core Controller (MY_Controller)

If you have multiple controllers needing this base config, using a core parent controller is a more object-oriented approach.

Step 1: Create MY_Controller

Make a file application/core/MY_Controller.php (CodeIgniter automatically loads this core extension):

<?php
class MY_Controller extends CI_Controller {
    protected function get_base_pagination_config() {
        return [
            'per_page' => 10,
            'full_tag_open' => '<nav class="pagination">',
            'full_tag_close' => '</nav>',
            'first_link' => '« First',
            'last_link' => 'Last »',
            'next_link' => 'Next →',
            'prev_link' => '← Previous',
            // Shared config here
        ];
    }
}

Step 2: Extend Your Controllers

Update your Posts and Categories controllers to inherit from MY_Controller instead of the default CI_Controller:

// In Posts.php
class Posts extends MY_Controller {
    public function index() {
        // Get the base config
        $pagination_config = $this->get_base_pagination_config();

        // Override unique params
        $pagination_config['base_url'] = site_url('posts/index');
        $pagination_config['total_rows'] = $this->post_model->count_all_posts();

        $this->pagination->initialize($pagination_config);

        // Your controller logic...
    }
}

This keeps your controller code clean and lets you update the base config once for all controllers that inherit from MY_Controller.

3. Use a Custom Helper Function

If you prefer a more procedural approach, a helper function works great too.

Step 1: Create a Pagination Helper

Make application/helpers/pagination_helper.php:

<?php
if (!function_exists('get_base_pagination_config')) {
    function get_base_pagination_config() {
        return [
            'per_page' => 10,
            'full_tag_open' => '<nav class="pagination">',
            'full_tag_close' => '</nav>',
            'first_link' => '« First',
            'last_link' => 'Last »',
            // Shared config options
        ];
    }
}

Step 2: Load the Helper & Use It

In your controller, load the helper and call the function:

// In Categories.php
public function index() {
    $this->load->helper('pagination');
    $pagination_config = get_base_pagination_config();

    $pagination_config['base_url'] = site_url('categories/index');
    $pagination_config['total_rows'] = $this->category_model->count_all_categories();

    $this->pagination->initialize($pagination_config);

    // Rest of your code...
}

Final Notes

All three methods work well—pick the one that fits your project's structure best:

  • Use the config file for simple, one-off centralization.
  • Use MY_Controller if you have multiple controllers sharing this (or other) base logic.
  • Use the helper if you prefer procedural code or need to access the config outside controllers (like in views, though that's less common).

内容的提问来源于stack exchange,提问作者Razvan Zamfir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:40:48