In the CodeIgniter documentation:
Simply create a new file called
pagination.php
, add the$config
array in that file. Then save the file inapplication/config/pagination.php
and it will be used automatically. You will NOT need to use$this->pagination->initialize()
if you save your preferences in a config file.
However, one of the pagination configurations is the base_url
, which presumably is going to be unique in every instance of pagination. For instance, if you use pagination for a controller function named yesterday
your base_url
might be something like /news/yesterday/pages/
. Then for a controller function named today
, your base_url
might be /news/today/pages/
. Given this scenario, you’ll need settings common for both instances stored in your pagination.php
file and unique settings declared at the time you create the pagination links.
Consider this code:
1 2 3 4 5 6 |
$config['base_url'] = '/news/yesterday/pages/'; // <- unique to this instance $config['uri_segment'] = 4; // <- depends on above setting $this->load->library('pagination'); $this->pagination->initialize($config); $data['pagination'] = $this->pagination->create_links(); |
In older versions of CodeIgniter, this code worked in conjunction with a pagination.php
config file. However, after upgrading my project to version 3.1.0, I discovered the styling of my pagination links were totally broken. The settings contained within pagination.php
were suddenly being ignored. Apparently the initialize()
function is resetting pagination to its defaults, disregarding the pagination.php
file entirely, and only using the $config
settings array passed into it.
I no longer see the use of having a pagination.php
file for common settings. If one of the core principals of CodeIgniter is DRY (Do not Repeat Yourself), then pagination.php
has been rendered useless in this regard. You’re going to have both common configuration settings for all pagination instances in your project along with settings that are unique to each instance. If having unique settings for multiple instances precludes being able to use pagination.php
, then its own purpose is already defeated. In order to use pagination.php
, every instance of pagination would need an identical URL structure, which makes it impossible to use on more than one Controller function.
Consider two scenarios that illustrate the issue:
1. Do not use pagination.php
file – Declare all settings for each instance of pagination by constructing the $config
array in every Controller function. Settings common to all instances of pagination will need to be repeated for each instance of pagination throughout your Controller functions. Does not adhere to DRY principles. Not ideal.
2. Use pagination.php
file – All pagination settings are contained in a central location adhering to DRY principles. However, since base_url
is also a pagination setting, you’ll be forced to use the same Base URL for all instances of pagination. Impossible.
Workaround:
1. Copy your common pagination settings into your custom configuration file contained within application/config/
directory. If you don’t already have a custom configuration file, create and auto-load it. Assign the array to a single configuration item named pagination
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// pagination $config['pagination'] = [ 'attributes' => array('class' => 'page'), 'full_tag_open' => '<p class="pagination">', 'full_tag_close' => '</p>', 'cur_tag_open' => ' <span class="page page-current">', 'cur_tag_close' => '</span>', 'first_link' => '«', 'last_link' => '»', 'next_link' => '›', 'prev_link' => '‹', 'num_links' => 6, 'use_page_numbers' => TRUE, 'per_page' => 16, // number of items per page ]; |
2. Access these common settings within your Controller by calling your new custom configuration item. Use a foreach
to loop through the pagination
settings array:
1 2 3 4 5 6 7 8 9 10 |
foreach ($this->config->item('pagination') as $key => $value) { $config[$key] = $value; } $config['base_url'] = '/news/yesterday/pages/'; // <- unique to this instance $config['uri_segment'] = 4; // <- depends on above setting $this->load->library('pagination'); $this->pagination->initialize($config); $data['pagination'] = $this->pagination->create_links(); |
One advantage of this method is that you can individually over-ride any specific setting by simply assigning it to the $config
array anytime after your foreach
loop.
1 2 3 4 5 6 7 |
foreach ($this->config->item('pagination') as $key => $value) { $config[$key] = $value; } $config['first_link'] = '←'; // <- overrides your custom setting $config['base_url'] = '/news/yesterday/pages/'; // <- unique to this instance $config['uri_segment'] = 4; // <- depends on above setting |
3. Delete the seemingly useless pagination.php
file!
You can merge config by using array_merge function, like this:
// Your settings
$config[‘per_page’] = 3;
// Add common settings
$config = array_merge($this->config->item(‘pagination’), $config,);
Your post is very helpful for me.
Thank you very much sir.