This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
TodoExpand, clarify, elaborate and more examples. Autoloading via Kohana::auto_load and naming rules.

Loading resources

Note:

Some libraries are always loaded automatically: URI, Router and Input.

Autoloading

PHP has functionality to automatically load files if a certain class has not been loaded yet. Kohana employs this functionality.

Example

$user = new User_Model;
$cache = new Cache();
html::stylesheet();
$this->layout = new View('layout');

Loading libraries

Libraries can be autoloaded:

$this->cache= new Cache;
echo $this->cache->get('mycache');

Some of the libraries (Calendar, Captcha, Image, ORM, Pagination, Validation, View) provide another method, factory, to load instantiate and use them. This way methods can be chained efficiently:

Examples:

View::factory('some_view')->set(array('title' => 'Welcome to Kohana !', 'breadcrumb' => 'Kohana > Welcome'))->render(true);
 
$post = Validation::factory($_POST)->pre_filter('trim')->add_rules('field1', 'length[2,15]','alpha_numeric');
 
$page = ORM::factory('page')->where('name', 'index')->find();
 
$image = Image::factory('moo.jpg')->resize(400, NULL)->crop(400, 350, 'top')->sharpen(20)->quality(75);

It is recommended to access the Database and Session objects via the singleton instance, rather than using new:

$db = Database::instance() and $s = Session::instance() Note: If the objects do not exist, they will be instantiated.

Example: Load Session and Database in a Base Controller, and access the objects in your extended controllers.

// Base Controller code
$this->db = Database::instance($db_group);
$this->session = Session::instance();
// Now in any controller which extends Base Controller
$var = $this->session->get('var');
$query = $this->db->query('SELECT * FROM `table`);

Loading database

Loading the database can also be done like this

$this->db = new Database;

Loading database in models

In models the database is loaded automatically as far as you call parent::__construct(); in your constructor.

public function __construct()
{
	// load database library into $this->db (can be omitted if not required)
	parent::__construct();
}

Loading helpers

Using a helper is fairly simple. Just call method as a static method. The class will be loaded automatically

echo url::base();
echo html::breadcrumb();

Loading views

Views are the final output of Kohana. They can be embedded into each other so as to help you in making your site. The actual rendering of a view is not done when you load one. More on views can be found on the views page. Information on the view class can be found on the View class page.

Example

$this->layout=new View('layouts/layout'); // will load the file views/layouts/layout.php
 
//will render the view
$this->layout->render(TRUE);

Loading models

Loading a model is the same as loading a view except you must append '_Model' to your model name. More can be found on the Models page.

For instance your model is User_Model, the filename would be models/user.php The loading of the model happens in the controller. Example

$user = new User_Model;
$name = $user->get_user_name($id); //get_user_name is a method defined in User_Model