So you decide to join the whole MVC hype that is currently happening throughout different language landscapes: This is a good move, you will get to learn why people believe using framework(s) not only makes you code faster, but also become a better software developer.
Although tutorials are targeted toward beginners and you don’t not require to have experience with MVC, however basic knowledge for MVC frameworks (and design pattern) may come in handy.
So what’s MVC?
Model View Controller (MVC) is an software architectural pattern that keeps application logic separate from the presentation and allows for cleaner and easier to work with code. Frameworks like Kohana promotes code separation, leading you broke down your concerns into:
- models that represents a piece of information we often refer as object
- views that focus on generating output display, either it’s html page, xml file or json response
- controllers that acts as central of commands that capture user requests, manipulate/fetch information and direct method of display to view
- helpers that modularise reusable codes in controller
- open source libraries that provides features you may use without writing from scratch. Such as form validator, image manipulation, user authentication etc.
Install Kohana
Start by download the latest stable version of Kohana framework, currently is 3.1.2.
Extract the folder under your virtual root.
If you have your PHP web server setup properly and extracted your kohana folder to the right location, you now should be able to go http://localhost/kohana/ at your web browser and you should see an environment test page showing whether your current server setup is ready to deploy kohana project or not.
Before your first line of code
To process, you will need to remove \install.php page which triggers environment testing.
Now setup some basic bootstraping to get you started. Open up \application\bootstrap.php and start by updating your time zone. Since I’m in Sydney, I will be changing to:
/** * Set the default time zone. * * @see http://kohanaframework.org/guide/using.configuration * @see http://php.net/timezones */ date_default_timezone_set('Australia/Sydney');
Set base_url property since we will be working from a directory, so the new value should be from ‘/’ to ‘/kohana’
/** * Initialize Kohana, setting the default options. * * The following options are available: * * - string base_url path, and optionally domain, of your application NULL * - string index_file name of your index file, usually "index.php" index.php * - string charset internal character set used for input and output utf-8 * - string cache_dir set the internal cache directory APPPATH/cache * - boolean errors enable or disable error handling TRUE * - boolean profile enable or disable internal profiling TRUE * - boolean caching enable or disable internal caching FALSE */ Kohana::init(array( 'base_url' => '/kohana', ));
And now, you should be good to go.
And your greeting…
Go visit http://localhost/kohana/ again and now you should see your first hello world!
That’s a bit disappointed isn’t it, that you don’t get to code anything! To make sure you know exactly what has happened there, take a look of your \application\bootstrap.php again and observe it’s route setting:
/** * Set the routes. Each route must have a minimum of a name, a URI and a set of * defaults for the URI. */ Route::set('default', '((/(/)))') ->defaults(array( 'controller' => 'welcome', 'action' => 'index', ));
It has (by default) specified that at home page of your web site, load ‘welcome’ controller and its ‘index’ action.
Now you can find your way to location of hello world, which is in file \application\classes\controller\welcome.php. let’s change our output message:
class Controller_Welcome extends Controller { public function action_index() { $msg = '<br /--> <h1>Hello World</h1> I just want to say this out and loud! '; $this->response->body($msg); } } // End Welcome
And refresh your web page…
Now we have it, your first hello world page written with Kohana framework.
Do you know
There’s no closing ?> tag in the php files you opened today. In fact if you check are php files in Kohana framework, it’s all like that! in the project. This is to prevent php script accidentally sending http header prematurely. More details were explained on a StackOverflow Question.
Summary
This is the first tutorial, will be simply straight forward. Pretty much test the water of MVC frameworks.





Pingback: Kohana Day 2: Basics with Controller and Action | Travis, Travaganza