Tag Archives: extend

Extending the CodeIgniter URI class to simplify pagination base_url option

In a lot of my controllers, I use pagination along with the HTML <base> tag’s href attribute for relative link URLs.

Example URL: http://www.domain.com/admin/users/index/pagination_offset

HTML Base tag: <base href=”http://www.domain.com/admin/”>

What this means, is I usually have to build pagination’s base_url option based on where I am and what information is being passed in the URI. A lot of times it ends up looking something like…

$this->load->library('pagination', array(
    'base_url'       => $this->uri->segment(2) . '/' . $this->uri->segment(3) . '/' . $this->uri->segment(4),
    'total_rows'     => $total_rows,
    'per_page'       => 20,
    'uri_segment'    => 4
));

What I decided to do what extend the core CodeIgniter URI class to provide a basic slice method to grab a chunk of the URI by applying PHP’s array_slice function to the segments property of the URI class. Continue reading Extending the CodeIgniter URI class to simplify pagination base_url option

Change CodeIgniter’s Form_validation error delimiters globally!

I have seen this question all over the web and am surprised that nobody has actually offered such a simple solution.

Anyway, here is a quick, simple and painless way to set CodeIgniter’s error delimiters (used by the Form_validation library) one time for your entire application. Create “application/libraries/MY_Form_validation.php” with the following contents… Continue reading Change CodeIgniter’s Form_validation error delimiters globally!

Easily load multiple views with CodeIgniter

It is not uncommon to load a common header, footer and other views in each controller. While this can be achieved like so…

$this->load->view('header', $header_data);
$this->load->view('content', $content_data);
$this->load->view('footer', $footer_data);

I wanted the ability to do the same thing with a single line of code. So I decided to extend the core Loader library by creating “application/core/MY_Loader.php” with the following code… Continue reading Easily load multiple views with CodeIgniter

Disable browser cache easily with CodeIgniter

I have a project where users are validated by session data. When the user logs out I destroy the session and redirect them to the log-in page, but this does not prevent them from clicking the browser’s “back” button, in which case they still see the data.

My first solution was to set headers with PHP’s header() function. Then I decided to use CodeIgniter’s setheader() function from the Output library (no real difference, just using CI methods when possible). Finally, I decided the best way to do this would be to extend the Output library itself. This way I am not repeating multiple function calls in each controller. Here is end-result… Continue reading Disable browser cache easily with CodeIgniter