Alex Rivera | Logout

Favourite Kohana Tips & Features?

Asked 2010-03-06T15:49:26.080
61

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.

Edit
Report

2 Answers

7

HMVC + AJAX = is_remote()

This function checks both - the internal and AJAX requests. It might be handy if some parts of the page are initially loaded using HMVC technique, and can be then reloaded with AJAX. Place it withing some base controller, from which you extend all your proper controllers (I call it 'base controller'):

public function is_remote()
{
    if ($this->request->is_initial())
    {
        if ($this->request->is_ajax())
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
    else
    {
        return TRUE;
    }
}

A shorter (equivalent) way of writing this:

public function is_remote()
{
    return ( ! $this->request->is_initial() || $this->request->is_ajax());
}

Hope this helps.

answered 2011-05-31T19:09:45.650
6

Display an error page

If you need to display an error page, Kohana has built in exceptions for it. Once you've thrown an exception, you can create a custom exception handler and have a HTML error page shown. You'll want a switch to show the real error in development.

Kohana 3.1

throw new HTTP_Exception_404('The article :article was not found', 
    array(':article' => $article->name));

The 2nd argument provides a way for you to replace strings in the error message.

Kohana 3.0

Doesn't have HTTP exceptions bundled. You should create your own exceptions and handle them. Kohana has a tutorial for this: Kohana - Custom Error Pages

answered 2011-05-22T19:13:47.923

Your Answer