Getting Started with Kohana 3, Part III – Initial Setup

I am no longer using Kohana in any of my personal or professional projects. I have moved to a better, more modular framework in Silex, which I will be writing about shortly. This series has been permanently discontinued.

This is Part III of a multi-part series. Below are the links to other parts of this tutorial!

In my Getting Started with Kohana 3, Part II – Bootstrap, we went through the bootstrap.php file and figured out what many things did. We also moved our routes into a separate file for easier management. In this part III, we’ll go through the actual controller and views to get some real output into your browser, as well as setting up a popular Kohana module, KOstache.

I’ve put all the code up on my GitHub account at https://github.com/jtreminio/Kohana-3-Tutorial. Please feel free to clone and send it pull requests. I will try to keep each chapter in its own branch, so you can easily follow along.

Cascading Filesystem

One of my favorite features of the Kohana Framework is its cascading filesystem. It’s a great, easy to use way of extending or completely overriding core or module classes with your own code.

Documentation may be found here.

The CFS is a core concept of the framework, so it would be good to go through that page until you completely grasp how it works, which in reality is very simple.

The Kohana Framework ships with a Date class, which can be instantiated with $date = new Date. But if you look at that class, which can be found at system/classes/date.php you’ll notice it’s an empty class that simply extends Kohana_Date which is found at system/classes/kohana/date.php. If you create a new file at application/classes/date.php and set up the class then Kohana’s autoloader would look to that class when instantiating a new Date object.

Modules

In Kohana, modules are simply placed in their own folder within the modules folder. Each module should have its own folder.

Documentation may be found here.

The Controller

Kohana comes with a default controller, Controller_Welcome, which we renamed to Controller_Home in a previous step. Visiting the base URL of your application runs Controller_Home::action_index(). All URLs must have a controller prepended with Controller_ and an action prepended with action_.

Documentation may be found here.

The Controller_Home class is extremely simple and minimal – just enough to get some output to the screen. It extends the Controller class which provides it with all the necessary tools to operate, include $this->response->body().

One thing to note is that controller action methods should never return anything – any reponse you want outputted to any type of request, be it API, user’s browser, or HMVC, should be handled with the $this->response->body() method.

You pass HTML, JSON or anything else to $this->response->body(). No need to echo as the framework handles all this for you. If you pass parameters it’ll consider that as the output to send requestor. If no parameters are passed, it’ll return what you’ve previously set (if anything).

class Controller_Home extends Controller {
    public function action_index()
    {
        $this->response->body('hello, world!');
    }
}

Continue reading


Getting Started with Kohana 3, Part II – Bootstrap

I am no longer using Kohana in any of my personal or professional projects. I have moved to a better, more modular framework in Silex, which I will be writing about shortly. This series has been permanently discontinued.

This is Part II of a multi-part series. Below are the links to other parts of this tutorial!

In my Getting Started with Kohana 3, Part I – Initial Setup, I walked you through the bare minimum to getting Kohana up and running on your local server. In this part II, I’ll take you through editing your bootstrap file and setting up your default router.

I’ve put all the code up on my GitHub account at https://github.com/jtreminio/Kohana-3-Tutorial. Please feel free to clone and send it pull requests. I will try to keep each chapter in its own branch, so you can easily follow along.

What is the bootstrap

The bootstrap is called on every single request to your Kohana application. In it you can set several options, such as default language, setting your app’s base URL, turning the index.php in your URL off/on, as well as choosing what, if any, modules you want to enable. By default routes are also included in this file.

This important file is located at application/bootstrap.php.

Environment Flag

Open the bootstrap file and change the following:

if (isset($_SERVER['KOHANA_ENV']))
{
    Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
}

Kohana::$environment = isset($_SERVER['KOHANA_ENV'])
    ? constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']))
    : Kohana::PRODUCTION;

What’s happening here is that we’re automatically setting our environment flag. Remember the KOHANA_ENV variable you set in your virtual hosts file in part I? It comes into play now. Kohana comes with 4 environments defined out of the box: PRODUCTION, STAGING, TESTING and DEVELOPMENT. You’re welcome to add more by extending the Core class (more on this in a future article) and adding in your own constant.

You can easily switch between flags by setting up a different virtual host for each flag (kohana-tutorial.dev, kohana-tutorial.test, etc), or simply changing the variable value in your one host.

Continue reading


Getting Started with Kohana 3, Part I – Initial Setup

I am no longer using Kohana in any of my personal or professional projects. I have moved to a better, more modular framework in Silex, which I will be writing about shortly. This series has been permanently discontinued.

This is Part I of a multi-part series. Below are the links to other parts of this tutorial!

The Kohana Framework started off as a fork of the very popular CodeIgniter Framework. During the KO 2.x versions, it stayed fairly similar to how CodeIgniter functioned, and a developer could easily jump from one to the other and feel very familiar. With the introduction of the Kohana 3.x version, this all changed, as Kohana was completely rewritten from the ground up. Now, unless you’re a seasoned PHP developer, you may have difficulty making the jump from other frameworks in Kohana. I have written this tutorial to be your step-by-step guide into the world of Kohana.

Who is this for?

This guide will be very low-level and easy to follow, but you must be an experienced PHP developer. If you have never used a framework before, but have a firm grasp on OO concepts, some patterns, and best practices, you should be fine. It assumes you have your local environment set up and are comfortable working with Apache to the extent of setting up your own virtual hosts.

If you have already started looking into Kohana but are lost, or are not sure what your next steps should be, I will attempt to take you to the next level by introducing must-have modules and best practices in both Kohana and MVC (actually, MVVM but we’ll get to that!) frameworks in general.

What will you learn?

Following this guide, you will learn how to create a blog from start to finish. Yes, a stupidly large majority of framework tutorials out there deal with blogs, but on the upside at least you know exactly what components make up a blog so it will make it much easier for you to follow along.

I will take you through initial setup of the framework, module installation and converting Kohana from MVC, where the V(iew) is simply a template that gets passed information from the C(ontroller), to the MVVM (often mistake as MVVC) design pattern, where we slim the down the C(ontroller) to basically handle route requests, and expand the responsibilities of the V(iew)M(odel) and implement true templates.

You will even learn something most other Kohana tutorials completely skip out on – HMVC!

By the end of this tutorial, we will have a fully functional, albeit lightweight blog setup that will hopefully make you comfortable with Kohana as a framework.

Requirements

I will assume you have PHP 5.3+ (I am running 5.3.10), Apache and MySQL. I will also be using Xdebug for debugging and PhpStorm as my IDE.

Why Kohana?

Kohana is not perfect, far from it (there, I said it). In fact, it has some architectural decisions that may not make perfect sense. Regardless, it’s one of my favorite frameworks, and I have the privilege of using it both at my full-time gig and all contract jobs I take on. Each version that is released by the dev team comes with some great improvements that keep polishing this great framework and simply makes it much more fun to work with.

About this tutorial

This tutorial will be made up of several chapters, each being bite-size so you can easily absorb the information presented (and so I don’t have to write a ton each time!).

I’ve put all the code up on my GitHub account at https://github.com/jtreminio/Kohana-3-Tutorial. Please feel free to clone and send it pull requests. I will try to keep each chapter in its own branch, so you can easily follow along.

Now that that very long-winded introduction is out of the way, let’s get started!

Continue reading