An Introduction to Kohana3 PHP Framework

Posted on December 12, 2010

This is a brief introduction into the Kohana framework. The next two tutorials will cover how to build a blog in Kohana.


Installing Kohana is very straight forward, just download the latest stable version of Kohana and unzip it. No need to mess around with yaml or xml files like other frameworks. Navigating to the directory on your web server should give you a page similar to…


The filesystem plays a big part in the operation of the Kohana framework. Kohana follows the HMVC pattern (hierarchical model view controller). This means that the same filesystem structure exists at multiple locations in the framework. The following directories exist in the application, system and in all module directories:

  • classes
  • config
  • i18n
  • messages
  • tests

When calling a class in Kohana, the autoloader checks if the class exists first in application/classes/ , then in each of the installed modules, classes subdirectory and finally in system/classes/. If the file cannot be found then an exception is thrown. Kohana follows the Zend_Class_Naming style. For example, if you instantiated a class named Hello_World, Kohana would look at each of the following locations in this order. If it finds that it exists, it will return.

  • application/classes/hello/world.php
  • modules/*moduleName*/classes/hello/world.php (it would check every installed module)
  • system/classes/hello/world.php


All configuration is stored in the config directory (Because of the HMVC filesystem, there are multiple config directories and the specific configuration file you specify can exist in any of them). Configuration files return an array. For example if we made the file application/config/hello.php and set the contents as

return array(

    'website' => 'Query7',
    'url' => ''


If we wanted to access the properties of this configuration file we would use the Kohana::config() method.


would return

return array(
    'website' => 'Query7',
    'url' => ''


and calling


would return



Modules provide additional features and can be easily added to your application. Kohana3 ships with some modules including database, orm and userguide (which contains documentation). To enable modules in your project open application/bootstrap.php and scroll to around line 78. There you will see:

    // 'auth'       => MODPATH.'auth',       // Basic authentication
    // 'cache'      => MODPATH.'cache',      // Caching with multiple backends
    // 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
    // 'database'   => MODPATH.'database',   // Database access
    // 'image'      => MODPATH.'image',      // Image manipulation
    // 'orm'        => MODPATH.'orm',        // Object Relationship Mapping
    // 'oauth'      => MODPATH.'oauth',      // OAuth authentication
    // 'pagination' => MODPATH.'pagination', // Paging of results
    // 'unittest'   => MODPATH.'unittest',   // Unit testing
    // 'userguide'  => MODPATH.'userguide',  // User guide and API documentation

To enable a module, just uncomment the line (remove the initial //). To install your own modules, put the un-archived module in your modules directory, and add a line to the array above. For example if you were installing the hive module (an ORM for Kohana), you would put the hive directory into your modules directory and add the following line to the array passed to Kohana::modules()

'hive' => MODPATH.'hive'

Now that the hive module is installed, the Kohana autoloader will add modules/hive/ to it’s path so it will check in that directory for classes and configuration files (if necessary).

Most modules come with their own configuration scripts. Because of the way the Kohana3 file system is structured, you can copy the default configuration script from modules/*name*/config/*script*.php and paste it in application/config/*script*.php and Kohana will immediately start using the configuration file located in your application directory. A good example of this is with the database module. The file modules/database/config/database.php contains all of the database connection information. Because this information is specific to our application it should be copied over to application/config/database.php and values edited appropriately. You do not need to delete the old modules/database/config/database.php because Kohana looks for application/config/database.php first and as soon as it finds it, it will return. Nothing in modules directory should contain anything specific to your application.

Modules can contain a file called init.php in the root directory (modules/*moduleName*/init.php). If the module is installed, this file is called every request.

Comments are closed.