Skip to main content
Common Functions

Common Functions

CodeIgniter ships global helper functions that cover the tasks you repeat in almost every application. This topic groups the ones you will use most.

URLs

echo base_url('assets/css/app.css');
echo site_url('products');
echo current_url();
echo route_to('product', 42);
echo url_to('Catalog::show', 42);

base_url() resolves assets and uploads. site_url() builds application URLs. route_to() and url_to() generate URLs from named routes or controller actions.

Output and forms

echo esc($title, 'html');
echo old('email');
echo csrf_token();
echo csrf_hash();

esc() prevents cross-site scripting. old() repopulates a form after validation fails. The CSRF functions produce the field name and value used in forms.

Application helpers

$value = env('app.timezone', 'UTC');
$mailer = service('email');
helper('text');
$config = config('App');

Diagnostics

log_message('info', 'Cache warmed for {count} items', ['count' => $count]);
$timer = \Config\Services::timer();
$timer->start('report');

Working with cache

$cache = service('cache');
$cache->save('home_stats', $stats, 300);

Command-line checks

if (is_cli()) {
    echo "Running from the command line\n";
}

These functions live in helpers that CodeIgniter loads for you. If a function is missing in a custom context, load its helper with helper('url') or the relevant helper name.