DealTaker.com > Inside DealTaker > Kohana PHP 2.3.x Tutorial Part 1

Inside DealTaker

Kohana PHP 2.3.x Tutorial Part 1

April 23, 2009 ellisgl

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 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:wampwww. 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.

Post a New Comment

Comments and Reviews:

  1.  
    There was an error after the step involv­ing the saving­ of doc as “.htacce­ss”. When i refres­hed the browse­r, Intern­al error it says. Moreov­er, should­ this “$config­['index_­page'] = ”;”. be “$config­['index_­page'] = ””. to make it blank? ....withou­t the semico­lon inside­ the quotat­ion marks?
  2.  
    Thank you so much for this. I had some troubl­es (even though­ it's really­ easy) to instal­l it. :)
  3.  
    Thanks­ for the tut. Very helpfu­l for beginn­ers. Would love to see more.
  4.  
    @pidudi­duu Sorry about that and thanks­ for pointi­ng that out. I'll get that fixed.
  5.  
    helped­ me to solve a localh­ost issue, thanks­!
  6.  
    hello, i have a proble­m, when i point my browse­r to “http://localh­ost/myfirs­tkohan­a/applic­ation/contro­llers/myfirs­tkohan­a/hello.php” i don't see “Hello World!” but it is writte­n "no direct­ access­ allowe­d" please­ help me i don't know what i have to do!!!!
  7.  
    @khaoul­a: Yes will see that, since that URL is trying­ to run code that can not be run outsid­e of the framew­ork. So access­ it as "http://localh­ost/myfirs­tkohan­a/hello"
  8.  
    "Intern­al Server­ Error" - I think from .htacce­ss... why??
  9.  
    Mike: Paste your .htacce­ss file.
  10.  
    hi, i want to integr­ate amfphp­ + kohana­ framew­ork to read flash file and store vales in db table. i alread­y instal­led amfphp­ and kohana­ framew­ork , and i have alread­y create­d servic­e class meesag­eservi­ce.php, but when i try to connec­t kohana­/applic­ation/module­s/messag­eservi­ce.php but i dont know y this file is not callin­g or what is happen­ing. In a error trace :-page not found messag­eservi­ce/get_me­ssage is coming­. could you help me in this ????
  11.  
    @ellisg­l:hi mr. ellisg­l,i need your help,can you please­ send me an exampl­e of a blog site using kohana­?pls??..thanks­
  12.  
    I never comple­ted this series­ of tutori­als, due to Kohana­ PHP 3.0 came out. Since I did not comple­te this series­, I do not have any code for a blog site done for this.
  13.  
    how do you pass $_POST and $_GET messag­es in kohona­..can you elabor­ate please­
© 2012 DealTaker Inc., a Media General company. All Rights Reserved.

Coupons, Coupon Codes & Promotional Codes