This is documentation for Kohana v2.3.x.
Status | Draft |
---|---|
Todo | Update for 2.2 proofread |
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.
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');
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.
config($key, $slash = FALSE, $required = TRUE)
retrieves a configuration item.
$key
specifies the item to fetch. A dot notation is used 'group_name.key_name' $slash
specifies if a /
must be added to the end of the item. Default is FALSE
$required
specifies if an item is required or not. Default is TRUE
$key
. May be string
, boolean
or array
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.
config_set($key, $value)
sets a configuration item.
$key
Specifies the key of the item to set. A dot notation is used 'group_name.key_name' $value
specifies the value of the item to set. May be string
or array
TRUE
if setting item succeeded, FALSE
if it failed.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''
config_load($name, $required = TRUE)
loads a configuration file from disk.
$name
Specifies the name of the config file (without any path information)$required
Specifies if the file to be loaded is required, default is TRUE
Kohana::config_load('locale');
config_clear($group)
clears a config group from the cached configuration
$group
Specifies the name of the configuration groupKohana::config_clear('locale');
include_paths($process = FALSE)
Retrieves the included file paths.
$process
Specifies if the include_paths
should be re-processed. Default is FALSE
APPPATH
first, all MODPATH
in the order configured, SYSPATH
last.$ipaths = Kohana::include_paths();
For customizing your applications logging. You can write errors or notices to the system log at run-time.
log($type, $message)
writes a formatted text message to the configured application log file.
$type
specifies the type of error to log. One of error
, alert
, info
, debug
$message
The message to be logged. Kohana::log('error', "email $email_id could not be sent"); // Kohana::log('info', 'Admin logged in successfully');
log_save()
Writes all current entries to the configured application log file. Typically there is no need to call this manually.
Kohana::log_save();
log_directory($dir = NULL)
Sets or retrieves the application logging directory.
$dir
Specifies the new directory to write log files to. Default is NULL
$dir
is NULL
the current log directory is returned, otherwise the directory specified in $dir
is returned.Kohana::log_directory();
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.
Kohana::instance(); //Example to load the input->get() method Kohana::instance()->input->get('id');
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.
Using Kohana::lang() languages strings can be retrieved.
Example
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'); //OR echo Kohana::lang('example.kohana_release', array('2.3.1'));
Searches for given key in a nested array. It takes:
Example
$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('levelone2.foo', $a); //Returns 'bar'
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.
Example
$controllers = Kohana::list_files('controllers', TRUE); // Now $controllers is an array containing paths to all controllers in your Kohana installation
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.
Example
// find a file named 'article.php' in the 'controllers' directory. include Kohana::find_file('controllers','article');
Example
// find a file named 'database.php' in the 'config' directory. include Kohana::find_file('config','database');
Example
// find a file named 'Swift.php' in the 'vendor' directory, sub-directory 'swift'. include Kohana::find_file('vendor','swift/Swift');
'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!
Example
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 Firefox 3.0 Windows XP 1 // (bool) true 1 // (bool) true