CodeIgniter Forums
How can I make this controller DRY? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: How can I make this controller DRY? (/showthread.php?tid=81169)



How can I make this controller DRY? - Ajax30 - 01-30-2022

I have been working on an online newspaper/blogging application with CodeIgniter 3.1.8 and Twitter Bootstrap 4. I am currently working on a lazy loading (of posts) feature.

I have added a pager option for the posts, to replace the pagination, if desired.

The code in the controller responsible for this is:

PHP Code:
if ($this->Static_model->get_static_data()['has_pager']) {
      $config['display_pages'] = FALSE;
      $config['first_link'] = FALSE;
      $config['last_link'] = FALSE;
      $config['prev_tag_open'] = '<li class="prev">';
      $config['prev_tag_close'] = '</li>';
      $config['next_tag_open'] = '<li class="next">';
      $config['next_tag_close'] = '</li>';
 } 
The problem is that it appears twice in the Posts controller alone and it also appears in other controllers (Categories, for instance) See the applications GitHub repo here

What is the most effective and DRY way to "externalize" the pager functionality (and the code above) and call it with a small piece of code, where it is nedded?


RE: How can I make this controller DRY? - sammyskills - 01-31-2022

At first glance, I will suggest you move the code to a helper OR a separate controller OR another library that extends the default CI3 pagination library.

That way, you can load the file wherever it is needed.