Kohana 3.1 Tips & Tricks

Kohana 3.1 has been out for sometime and looking pretty stable. Although compatibility with many Kohana 3.0 modules is broken, it requires little work to get the code working with the 3.1 branch. Some of the tips included here may also work with version 3.0 also, while some will only be effective when using the latest 3.1 branch.

1. Multiple configuration files for development/live websites

It is often desired development machinesto use a particular configuration, while use a different set of config on the live website. With Kohana 3.1, this is as easy as an apple pie. Use the following code in your application/bootstrap file, just after the Config_file construction code ( find the first line in the code below in your bootstrap file, and replace it with the complete code:

Kohana::$config->attach(new Config_File);
if (Kohana::$environment === Kohana::DEVELOPMENT)
    Kohana::$config->attach(new Config_File('config/local'));

Now create a folder called 'local' in the application/config folder, and copy all files from the 'config' folder in the 'local' folder. As long as you are working in your development mode, configuration will be first loaded from the 'local' folder, and then from the default config folder.

If you use the above code, you get an extra advantage when delpoying your site to live servers. You can upload the complete config folder as it is, without any change, and the files in the 'local' folder will be completely ignored!

Using $this->request

Although the temptation is to use the Request class static function Request::instance() in your controllers and views, using the 'request' property of the controller inside controllers is the fastest way to get information about the current request.

Date Helper functions

The Kohana Date helper functions mostly take a unix timestamp as argument, and return some or all of it formatted in a particular format. You can save yourself some time consuming date processing by storing your date/time field as timestamp in your database. Now you can pass the data from your queries directly to Kohana Date helper function to get instant formatting results. One of the coolest function is fuzzy_span(), which return timestamp formatted in twitter style such as '3 minutes ago' or '2 days ago'

Loading Modules when required

While most of the modules loaded in bootstrap.php are required for the application lifetime, some are not. Loading those modules in bootstrap is a simple waste of processing time. For example, an auth module might be required on every page to check for user's login status, but the user info module is only required when editing user's profile. Here's a simple way to load the user profile module in your Profile controller's before method (remember to remove the module from your bootstrap.php)

public function before() {
   parent::before();
   $mods = Kohana::modules();
   $mods['user_profile'] = MODPATH . 'user_profile';
   Kohana::modules($_mods);
   unset($mods);
}

Now the user profile module is only loaded when you require the profiles to be edited. Saves a considerable amount of filesystem search per page in large applications!

That's it for this time, keep tuned into our blog for more info & tips about the latest frameworks and development.

Posted: 08 Jul 2011
Blog comments powered by Disqus