CodeIgniter Libraries
Piece
Custom libraries for CodeIgniter, the PHP web application framework from EllisLab.
Output (extends native)
The CI core Output class provides, amongst other things, basic caching functionality. This extension adds support for cached headers and premature cache deletion.
Cached Headers
This feature is more of a bug fix in my opinion. It was written by Arjen van Bochoven and posted on the CodeIgniter wiki. It makes the caching provided by CI more accurate by storing and honouring custom headers (set using the native set_header()
) along with the rendered output.
Cache Deletion
A new function $this->output->clear_cache($uri)
takes a local URI as its argument and deletes the cached resource for that URI, forcing a update on the next access. Example use case: clear the cache for a blog entry when a new comment is posted.
/* clear_cache() example */
// 'Blog' controller
function entry($entry_id) {
...
if (/* valid entry_id */) {
$this->output->cache(60);
$this->load->view(...);
}
}
function add_comment($entry_id) {
...
if (/* comment added successfully */) {
$this->output->clear_cache("blog/entry/$entry_id");
}
}
Encrypt (extends native)
Quite simply, this extension provides URL–safe two-way data encryption.
Two-way data encryption is a handy feature to have at one’s disposal. It can be used to obfuscate publicly exposed data, for instance to provide unique auto-login URLs by encoding unique user information into the URL. CI comes with a handy Encryption class to do just that. However, the base64 encoder it uses can produce strings containing characters disallowed in default CI URLs.
The extension sanitises the native encode()
function’s output to produce URL–safe strings, and reverses the operation for decode()
.
Css
This Css, né CSS library contains two pre-processing functions useful when dealing with Cascading Style Sheets for web apps.
Minify
A very simple CSS code minimiser that removes redundant whitespace. Useful to compress CSS files on the fly and save bandwidth.
Prefix URLs
Gives the ability to prefix all URLs in the CSS code that begin with a forward slash (/
) with a base_url
of your choice. Defaults to using the base_url
specified in the CI application’s config/config.php
file.
Consider the following example:
/* expandUrls() example */
$css_code = <<<END
/* Museo family by Jos Buivenga
* Available for the low price of free at http://www.josbuivenga.demon.nl/ */
@font-face {
font-family: 'Museo';
src: url(/fonts/Museo300-Regular.otf) format("opentype");
font-weight: 300;
}
@font-face {
font-family: 'Museo';
src: url('/fonts/Museo500-Regular.otf') format("opentype");
font-weight: normal, 500;
}
END;
$css_code = $this->css->expandUrls($css_code, "http://static.example.com/media");
$this->output->set_output($css_code);
The resulting output would be:
/* Museo family by Jos Buivenga
* Available for the low price of free at http://www.josbuivenga.demon.nl/ */
@font-face {
font-family: 'Museo';
src: url(http://static.example.com/media/fonts/Museo300-Regular.otf) format("opentype");
font-weight: 300;
}
@font-face {
font-family: 'Museo';
src: url('http://static.example.com/media/fonts/Museo500-Regular.otf') format("opentype");
font-weight: normal, 500;
}
Usage
Dropping the files inside your application’s libraries
folder should, in most cases, be all you need to do. If you have changed your $config['subclass_prefix']
from the default 'MY_'
value, the class– and filenames of the native library extensions will need to be modified accordingly. The libraries and core system classes CI user guide pages provide more information.
License
With the exception of ‘Output’, all libraries on this page use the MIT License, which basically means you’re free to do what you want with it. The ‘Output’ library contains code by Arjen van Bochoven, which, by virtue of being posted to the CI wiki falls under the Creative Commons Attribution-Share Alike license.