Favourite Kohana Tips & Features? [closed]

Inspired from the other community wikis, I'm interested in hearing about the lesser known Kohana tips, tricks and features.

  • Please, include only one tip per answer.
  • Add Kohana versions if necessary.

This is a community wiki.


Solution 1:

Generating Form::select() options from database result

Kohana 3.1 and 3.0

$options = ORM::factory('model')
 ->order_by('title','ASC')
 ->find_all()
 ->as_array('id','title');

$select = Form::select('name', $options);

It should be noted this is not restricted to the ORM and can be used on all database results (they all support as_array). See the database results information for more details.

If you want to add a default option:

$options = Arr::merge(array('Please select a value.'), $options);

Solution 2:

Show last query executed

Kohana 3.1 and 3.0

echo Database::instance()->last_query

Taken from In Kohana 3, how do you figure out errors made during a query?.

Solution 3:

Set Kohana::$environment

Paste these lines to your .htaccess:

SetEnvIf SERVER_ADDR "^(127\.0\.0\.1|::1)$" KOHANA_ENV=development
SetEnvIf SERVER_ADDR "^((?!127\.0\.0\.1|::1).)*$" KOHANA_ENV=production

now, if you're on localhost, you are in development mode, otherwise you're in production mode

Edit: Added support for IPv6

Solution 4:

The difference between this->request->route->uri() and this->request->uri() (Kohana 3)

// Current URI = welcome/test/5 
// Using default route ":controller/:action/:id"

// This returns "welcome/test/5"
echo $this->request->uri(); 

// This returns "welcome/test1/5"
echo $this->request->uri(array( 'action' => 'test1' )); 

// This returns "welcome/index"
echo $this->request->route->uri();

// This returns "welcome/test1"
echo $this->request->route->uri(array( 'action' => 'test1' ));

As you can see, $this->request->route->uri() uses current route defaults (id is null), while $this->request->uri() applies current uri segments.