Kohana hello world

It seems unnecessary to write a Hello World tuorial for a PHP framework, because when someone comes to framework, he is no longer a beginner, can figure out his own Hello World. But this idea comes up in my brain, and I guess it might be helpful for someone who is the first time to work with MVC or OOP. It’s quite simple, let’s get started.

You should have installed Kohana correctly, now we create a hello.php in /application/controllers/, here is the content of the file:

<?php
/**
* Hello World controller
*/
class Hello_Controller extends Template_Controller
{
function __construct()
{
parent::__construct();
}

function index()
{
$this->template->title = “Hello World”;
$this->template->content = “Hello, welcome to Kohana World !!!”;
}
}
?>

We have a Hello Controller with a index method, so we can call this page in the URL: localhost/kohana/index.php/hello. If you cannot understand this, you may want to see ‘Show me, don’t tell me‘ section in Kohana 101. In the Hello Controller, we set the title and content of our Hello World page, of course you can change the title and content into anything.

Now we have a Controller, we also need a View to reveal the page. Create a template.php in /applicaion/views/, and type in the fellowing codes:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>

<head>

<title><?php echo $title ?></title>

<body>

<h2><?php echo $title ?></h2>

<p><?php echo $content ?></p>

</body>
</html>

Very simple HTML codes, echo the title and content set in the Hello Controller to the page, that’s all what it does. You can see how Kohana works, processing in Controller, sending the result to View, and the View exhibit the page.

Usually I’d like to add a profiler to a example page, so that we can see some system information. It’s very easy to do that, just add one line to the Construt function in the Controller:

$this->profiler = new Profiler;

So the Hello Controller file should look like this:

<?php
/**
* Hello World controller
*/
class Hello_Controller extends Template_Controller
{
function __construct()
{
parent::__construct();
$this->profiler = new Profiler;
}

function index()
{
$this->template->title = “Hello World”;
$this->template->content = “Hello, welcome to Kohana World !!!”;
}
}
?>

We can now visit localhost/kohana/index.php/hello, you will have a page the same as the image above.

You already have some basic concepts of Kohana now, I highly recommand you to read Kohana 101, or if you are even not sure how to setup your own Kohana, you can read my post, Install Kohana on Windows. Hope this can help you.

8 Responses to “Hello World Tutorial for Kohana”

  1. luf

    can you help me, it isn’t work on my browser I don’t know why,when I write localhost/kohana it’s work but when I tried to your example it isn’t.
    i used xampp as server, thx

  2. Zack

    Try to check these:
    1. The view file should be in folder /applicaion/views/, and should be named as template.php.
    2. The hello.php should be in /application/controllers/.
    3. Have you done some other configuration, for example, remove index.php from your URL?
    3. What do you get, when you visit localhost/kohana/index.php/hello in your browser?

  3. mozink

    hello… nice post…

    can you give me some tutorial like how to displaying database(mysql) table in browser?

    how much file must be created in controllers and views?

    pls give me example controller

    thank

  4. telesnake45

    Thank you very much. I was wondering how to start with Kohana. I was giving a try to CodeIgniter but i found it a little buggy with PostgreSQL.

Leave a Reply