subscribe to the RSS Feed

Wednesday, March 17, 2010

Kohana PHP 2.3.x Tutorial Part 1

Posted by ellisgl on April 23, 2009

So after reading my article about frameworks, you might want to try one out. Today we are going start a series of tutorials on how to use the Kohana PHP framework beginning with how to install it and creating a controller.

Before we get into this tutorial, lets go over what will be required.

Lets get started!
Download:
Load http://www.kohanaphp.com in your favorite browser and click the download link. On this page, click the “Download Kohana!” button. You should be prompted to save a file. Go a head and save the file some where. At the time of this writing, the version was at 2.3.2.

Install:
Open the file we just downloaded in your favorite archive program and extract it to a temporary location. Open that temporary location and you should have a folder that is named “Kohana_v2.3.2″ or something like that. Open that folder. Open a new window and open the root directory of your *AMP install. Since I’m using WAMP Server – mine is C:\wamp\www\. Next make a new folder in there named “myfirstkohana”. Copy the files from the “Kohana_v2.3.2″ directory to the “myfirstkohana”. Make sure your *AMP installation is up and running then point your browser to http://yourserver/myfirstkohana/ . You should have a screen stating that everything is “OK”.

If everything is “OK”, then remove or rename the “install.php” file in the “myfirstkohana” directory. Next we want to navigate into the application folder, then in the the config folder. Open the file named “config.php” in your favorite editor. There should be a line that reads: “$config['site_domain'] = ‘/kohana/’;” (Line 7 for me). We need to change the “/kohana/” part to reflect our install. So replace “/kohana/” with “/myfirstkohana/”. Also search for “$config['index_page'] = ‘index.php’;” and make the “index.php” part blank, so that line (Line 21 for me) should now read “$config['index_page'] = ”;”. Save that file.

Create a new document, cut and paste the following into it:

# Turn on URL rewriting
RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/kohana/, use /kohana/
RewriteBase /myfirstkohana/

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

Save this file in the “myfirstkohana” directory as “.htaccess”.

These changes will help with creating clean URLs that help with SEO efforts.

Reload the browser page and you should get something like this:

If everything looks good, then we have successfully installed Kohana PHP. Now lets make it do stuff!

My first controller:
Open a new document and put the following into it:

<?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Controller
 {
  public function index()
   {
    echo 'Hello World!';
   }
 }

Save this in “myfirstkohana/application/controllers” as “hello.php”. Here’s a little explaination of what we did. The line “defined(’SYSPATH’) or die(’No direct access allowed.’);” basically prevents the file from being executed by itself. The next line “class Hello_Controller extends Controller” creates a class called “Hello_Controller” that extends the controller object. The rules to follow for creating controllers is as follows:

  • Must reside in a controllers (sub-)directory
  • Controller filename must be lowercase, e.g. articles.php
  • Controller class must map to filename and capitalized, and must be appended with _Controller, e.g. Articles_Controller
  • Must have the Controller class as (grand)parent
  • Controller methods preceded by ‘_’ (e.g. _do_something() ) cannot be called by the URI mapping

stolen from: Kohana Docs

The first method, “index” is the default action. Pretty easy right? If you point your browser to “http://yourserver/myfirstkohana/hello/” you should see “Hello World!” on the screen now.

What if you wanted to exend the controller to do something else? Let add another method in the class that looks like:

public function america()
 {
  echo 'Hello America!';
 }

Now the hello.php should look like:

<?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Controller
 {
  public function index()
   {
    echo 'Hello World!';
   }

  public function america()
   {
    echo 'Hello America!';
   }
 }

Save this and open “http://yourserver/hello/america/”. Notice that is now show “Hello America!” on the screen. That wasn’t too hard, but lets make this a little more dynamic with arguements. Make a new method that will look like this:

public function name($name)
 {
  echo 'Hello ',$name,'!';
 }

Load up “http://yourserver/hello/name/monkey” and you should see “Hello monkey!” on your screen. Pretty straight forward. Let make it even more ‘friendly’. Add this to the top of the class:

public function __call($method, $arguments)
 {
  $this->name($method);
 }

If we load up “http://yourserver/myfirstkohana/hello/MrGuy/” we should get the same thing as if we loaded “http://yourserver/myfirstkohana/hello/name/MrGuy/” which would be “Hello MrGuy!”.

That’s it for this lesson. Next time we’ll be looking over Views.
Click here to download the files and don’t forget to check out some great computer and electronic deals.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
  • StumbleUpon
  • Mixx
  • Google Bookmarks
  • Reddit
  • Technorati
  • Yahoo! Buzz
  • MySpace
  • LinkedIn
  • Live
  • email
  • Print

  • pidudiduu said,

    There was an error after the step involving the saving of doc as “.htaccess”. When i refreshed the browser, Internal error it says.

    Moreover, should this “$config['index_page'] = ”;”. be “$config['index_page'] = ””. to make it blank? ….without the semicolon inside the quotation marks?

  • Per said,

    Thank you so much for this. I had some troubles (even though it’s really easy) to install it. :)

  • stmonkey said,

    Thanks for the tut. Very helpful for beginners. Would love to see more.

  • ellisgl said,

    @pidudiduu Sorry about that and thanks for pointing that out. I’ll get that fixed.

  • Jon Tao said,

    helped me to solve a localhost issue, thanks!

Add A Comment

home | top