Hello World Tutorial for Kohana
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.
.
.
-
http://lufcenter.wordpress.com luf
-
http://blogiii.com/zack Zack
-
http://nindita.com mozink
-
telesnake45
-
http://breaktime.yolasite.com Tash Pemhiwa
-
Pluto
-
porno





