^Status|Draft|
^Todo|Update 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.
header('Content-type: text/html; charset=iso-8859-1');
===== Methods (Config) =====
For working with [[general::configuration|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.
* **[string]** ''$key'' specifies the item to fetch. A dot notation is used 'group_name.key_name'
* **[boolean]** ''$slash'' specifies if a ''/'' must be added to the end of the item. Default is ''FALSE''
* **[boolean]** ''$required'' specifies if an item is required or not. Default is ''TRUE''
* returns **[mixed]** The value of the configuration item for ''$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.
==== Set config item ====
''config_set($key, $value)'' sets a configuration item.
* **[string]** ''$key'' Specifies the key of the item to set. A dot notation is used 'group_name.key_name'
* **[mixed]** ''$value'' specifies the value of the item to set. May be ''string'' or ''array''
* returns **[boolean]** ''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''
==== Load a config file ====
''config_load($name, $required = TRUE)'' loads a configuration file from disk.
* **[string]** ''$name'' Specifies the name of the config file (without any path information)
* **[boolean]** ''$required'' Specifies if the file to be loaded is required, default is ''TRUE''
* returns **[array]** The specified config file data.
Kohana::config_load('locale');
==== Clear cached config ====
''config_clear($group)'' clears a config group from the cached configuration
* **[string]** ''$group'' Specifies the name of the configuration group
* returns **[void]**
Kohana::config_clear('locale');
==== Get include paths ====
''include_paths($process = FALSE)'' Retrieves the included file paths.
* **[boolean]** ''$process'' Specifies if the ''include_paths'' should be re-processed. Default is ''FALSE''
* returns **[array]** File paths. ''APPPATH'' first, all ''MODPATH'' in the order configured, ''SYSPATH'' last.
$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.
* **[string]** ''$type'' specifies the type of error to log. One of ''error'', ''alert'', ''info'', ''debug''
* **[string]** ''$message'' The message to be logged.
* returns **[void]**
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.
* returns **[void]**
Kohana::log_save();
==== Log directory ====
''log_directory($dir = NULL)'' Sets or retrieves the application logging directory.
* **[string]** ''$dir'' Specifies the new directory to write log files to. Default is ''NULL''
* returns **[string]** If ''$dir'' is ''NULL'' the current log directory is returned, otherwise the directory specified in ''$dir'' is returned.
Kohana::log_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.
Kohana::instance();
//Example to load the input->get() method
Kohana::instance()->input->get('id');
==== 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
==== backtrace ====
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.
**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 [[http://es.php.net/manual/en/function.sprintf.php|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'));
==== find key strings ====
Searches for given key in a nested array. It takes:
* [string] Key to search for. Should be in form of 'rootnode.childnode.anothernode'
* [array] Array to search in. Should be an array of values and/or another arrays to represent nodes
**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'
==== Listing files ====
Iterates through all directories of a given name and returns found files. It takes two parameters:
* [string] In which directory to search in
* [bool] Should find_files be recursive? (TRUE or FALSE, defaults to FALSE)
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
==== finding files ====
Find a resource file in a given directory using Kohana's cascading filesystem.
* [string] Directory to search in
* [string] Filename to look for (excluding extension)
* [bool] Is the file required, throws an error if this is true and the file cannot be found (defaults to FALSE)
* [mixed] Custom file extension as string or FALSE to use default extension (defaults to FALSE)
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 [[:general: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 info ====
'user_agent' returns information on the user agent of the current request. It takes:
* [string] key or test name, default 'agent'.
__Available keys__: ''agent, browser, version, platform, mobile, robot, referrer, languages, charsets''.
__Available tests__: ''is_browser, is_mobile, is_robot, accept_lang, accept_charset''.
* [string] used with "accept" tests and contains the string to compare: user_agent('accept_lang', 'en)
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
<< [[core:event|Event]] : Previous | Next : [[core:utf8|Unicode]] >>