This is documentation for Kohana v2.3.x. For v3.x documentation, see .

Table of Contents
TodoUpdate for 2.2 proofread

Kohana Class

The Kohana class is at the center of Kohana. It loads up the Router, dispatches to the controller and does the final output.

This class provides static methods for retrieving or setting items related to: Configuration, Logging, User Agent

This functionality was previously provided by the Config, Log and User_Agent Libraries respectively.

Character set

The class also sets a utf-8 header. If you want a different charset you can override it by placing this in for example your controller. The browser will interpret the page with the new charset.

header('Content-type: text/html; charset=iso-8859-1');

Methods (Config)

For working with Config files. You can retrieve and set configuration items at run-time.

Configuration items are entries in the main config array and are referenced by; 'group.key' a dotted key notation.

Retrieve config item

config($key, $slash = FALSE, $required = TRUE) retrieves a configuration item.

Kohana::config('core.output_compression'); //returns boolean TRUE if output compression is enabled in the main application config file.
Kohana::config('session.driver'); //returns the string value for the current driver of the loaded Session.

Set config item

config_set($key, $value) sets a configuration item.

Kohana::config_set('core.output_compression', FALSE); //returns boolean TRUE if output compression is disabled in core config.
Kohana::config_set('session.driver', 'cookie'); //returns FALSE if session driver config could not be set to ''cookie''

Load a config file

config_load($name, $required = TRUE) loads a configuration file from disk.


Clear cached config

config_clear($group) clears a config group from the cached configuration


Get include paths

include_paths($process = FALSE) Retrieves the included file paths.

$ipaths = Kohana::include_paths();

Methods (Log)

For customizing your applications logging. You can write errors or notices to the system log at run-time.

Write a log message

log($type, $message) writes a formatted text message to the configured application log file.

Kohana::log('error', "email $email_id could not be sent");
Kohana::log('info', 'Admin logged in successfully');

Save all log entries

log_save() Writes all current entries to the configured application log file. Typically there is no need to call this manually.


Log directory

log_directory($dir = NULL) Sets or retrieves the application logging directory.


Methods (Other)

Initialize and Load Kohana superobject

Loads the controller and initializes it. Runs the pre_controller,post_controller_constructor, and post_controller events. Triggers a system.404 event when the route cannot be mapped to a controller.

//Example to load the input->get() method

Debugging information

Returns HTML formatted strings of variables that can be echoed to screen nicely.

echo Kohana::debug($this->input->post());

Prints out the POST variable


Will be called when an error occurs. It displays an overview of files and functions called so you can spot the source of the error. Very useful for debugging.

Use language strings

Using Kohana::lang() languages strings can be retrieved.


echo Kohana::lang('cache.resources');
//outputs is locale is set to en_US
//  Caching of resources is impossible, because resources cannot be serialized.
//If locale is set to de_DE
//  Das Cachen von Ressourcen ist nicht möglich, da diese nicht serialisiert werden können.

In the case of en_US, Kohana::lang('cache.resources') maps to i18n/en_US/cache.php and within this file to $lang['resources']

Kohana also allows to give extra arguments to Kohana::lang(), allowing us to use a formatted string (using the format specified in php sprintf function

Example In myapp.php i18n lang file:

$lang = array
  'kohana_release' => 'The last stable release of Kohana is %s'

In our controller, view, model…

echo Kohana::lang('myapp.kohana_release', '2.3.1');
echo Kohana::lang('example.kohana_release', array('2.3.1'));

find key strings

Searches for given key in a nested array. It takes:


$a = array
    'levelone1' => array
        'leveltwo1' => array
            'a' => 'aaa',
            'b' => 'aab',
            'c' => 'aac'
        'leveltwo2' => array
            'a' => 'aba',
            'b' => 'abb',
            'c' => 'abc'
    'levelone2' => array
        'a' => 'ba',
        'b' => 'bb',
        'foo' => 'bar'
Kohana::key_string('levelone1.leveltwo1.b', $a);
//Returns 'aab'
Kohana::key_string('levelone1.leveltwo2.c', $a);
//Returns 'abc'
Kohana::key_string('', $a);
//Returns 'bar'

Listing files

Iterates through all directories of a given name and returns found files. It takes two parameters:

It returns array of file paths to found files.


$controllers = Kohana::list_files('controllers', TRUE);
// Now $controllers is an array containing paths to all controllers in your Kohana installation

finding files

Find a resource file in a given directory using Kohana's cascading filesystem.

Returns an array if the type is i18n or a configuration file. When file is found it returns a string with the path to the file. It will return FALSE if the file is not found.

This method uses the cascading filesystem of Kohana, this means it will first look in the application directory to see if a file exists, then any module that exists in order they are supplied in the config.php file and then the system directory. Exception to this is the i18n files and the config files. They are loaded from the system directory upwards. Result is that you can copy half a language file from the system directory and place it in the application directory, variables declared in the system directory will be supplanted by the one in the application directory.


// find a file named 'article.php' in the 'controllers' directory.
include Kohana::find_file('controllers','article');


// find a file named 'database.php' in the 'config' directory.
include Kohana::find_file('config','database');


// find a file named 'Swift.php' in the 'vendor' directory, sub-directory 'swift'.
include Kohana::find_file('vendor','swift/Swift');

user agent info

'user_agent' returns information on the user agent of the current request. It takes:

Available keys: agent, browser, version, platform, mobile, robot, referrer, languages, charsets.

Available tests: is_browser, is_mobile, is_robot, accept_lang, accept_charset.

Be careful! Even if global XSS filtering is on, the data returned by this function will not be filtered!


print Kohana::user_agent();
print Kohana::user_agent('browser');
print Kohana::user_agent('version');
print Kohana::user_agent('platform');
print Kohana::user_agent('is_browser');
print Kohana::user_agent('accept_lang', 'en');

This could result in HTML as:

Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9) Gecko/2008052906 Firefox/3.0
Windows XP
1 // (bool) true
1 // (bool) true

« Event : Previous | Next : Unicode »

core/kohana.txt · Last modified: 2009/08/21 07:21 by pedro_sland