Kohana Tutorial: For the beginners

////Kohana Tutorial: For the beginners

Kohana Tutorial: For the beginners

Kohana is the framework I liked best among other PHP framework. Its an easy one also ;). To me it is organized, highly object oriented, having clear separation of model, view & controller.  Autoloading of classes is nice feature here also. Here is the tutorial I prepared. In a simple and short…

  • It is going to be a 3 or 4 part series
  • All series will have around 10+/- steps
  • I will be creating a simple blog with this example

Probably you have downloaded the zip file from here. Below are the steps one by one. So lets start…

Installation

  • Extract the zip file in htdocs folder
  • Rename it to [highlight type=”special_text”]kohana-blog[/highlight]
  • Now hit the url: http://localhost/kohana-blog/
  • You should see your installation info
  • If you are using linux, make [highlight type=”special_text”]kohana-blog/application/logs[/highlight] & [highlight type=”special_text”]kohana-blog/application/cache[/highlight] folder writable
  • You are done

Initial configuration

  • Now remove/rename [highlight type=”special_text”]kohana-blog/install.php[/highlight].
  • Change [highlight type=”special_text”]kohana-blog/application/bootstrap.php[/highlight] as same as below.

[php highlight=”2″ title=”kohana-blog/application/bootstrap.php”]
Kohana::init(array(
‘base_url’ => ‘/kohana-blog’,
));
[/php]

Get introduce with action & controller

By default URL format for koahna is like [highlight type=”special_text”]http://<base_url>/index.php/<controller>/<action>[/highlight]. So for an example http://localhost/kohana/index.php/article/new means it will be looking for [highlight type=”special_text”]article controller (Controller_Article)[/highlight] and execute [highlight type=”special_text”]new action (action_new())[/highlight]. If action part is empty, [highlight type=”special_text”]index action[/highlight] will be executed ;).

  • controller & action in Kohana:

[php highlight=”3,5,6″ title=”kohana-blog/application/classes/controller/welcome.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Controller_Welcome extends Controller {

public function action_index() {
$this->response->body(‘hello, world!’);
}

} // End Welcome
[/php]

  1. Here [highlight type=”special_text”]Controller_[/highlight] is the prefix for welcome controller & it extends Controller
  2. Here [highlight type=”special_text”]action_[/highlight] is the prefix for an action
  3. [highlight type=”special_text”]$this->response->body(‘hello, world!’);[/highlight] sets [highlight type=”special_text”]hello, world![/highlight] as the response for the request.
  • Lets add an action as highlighted below

[php highlight=”9,10,11″ title=”kohana-blog/application/classes/controller/welcome.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Controller_Welcome extends Controller {

public function action_index() {
$this>response->body(‘hello, world!’);
}

public function action_another() {
$this->response->body(‘added another action…’);
}

} // End Welcome
[/php]

Now try this url: http://localhost/kohana-blog/index.php/welcome/another

  • Lets create an article controller for our blog as below.

[php title=”kohana-blog/application/classes/controller/article.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Controller_Article extends Controller {

public function action_index() {
$this->response->body("Kohana Blog");
}

}
[/php]

Now try this url: http://localhost/kohana-blog/index.php/article/index

Render a view with Kohana

  • Create the [highlight type=”special_text”]kohana-blog/application/views/article/index.php[/highlight] file under [highlight type=”special_text” ]application/view[/highlight] folder as shown below

[php htmlscript=”true” title=”kohana-blog/application/views/article/index.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’); ?>

<h1>Kohana Blog Homepage</h1>
[/php]

  • Modify the [highlight type=”special_text”]Controller_Article[/highlight]’s method [highlight type=”special_text”]action_index()[/highlight] as below to load the view

[php highlight=”6,7″ title=”kohana-blog/application/classes/controller/article.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Controller_Article extends Controller {

public function action_index() {
$view = new View(‘article/index’); // loads the ‘article/index.php’ file under ‘application/view’ folder
$this->response->body($view); // render the view as response
}

}
[/php]

Now try this url again: http://localhost/kohana-blog/index.php/article/index

Now prepare database configuration

  • Run following sql script. It will create simple articles table in the database.

[sourcecode language=”plain” title=”kohana-blog/kohana-blog.sql”]
CREATE SCHEMA `kohana_blog`;

use `kohana_blog`;

CREATE TABLE `kohana_blog`.`articles` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
`content` text,
PRIMARY KEY (`id`)
);
[/sourcecode]

We will use Kohana ORM to communicate with database. So both database & orm module is needed to be enabled. we can do so modifying [highlight type=”special_text”]kohana-blog/application/bootstrap.php[/highlight] as shown below.

[php highlight=”8,10″ gutter=”false” title=”kohana-blog/application/bootstrap.php”]
/**
* Enable modules. Modules are referenced by a relative or absolute path.
*/
Kohana::modules(array(
// ‘auth’ => MODPATH.’auth’, // Basic authentication
// ‘cache’ => MODPATH.’cache’, // Caching with multiple backends
// ‘codebench’ => MODPATH.’codebench’, // Benchmarking tool
‘database’ => MODPATH.’database’, // Database access
// ‘image’ => MODPATH.’image’, // Image manipulation
‘orm’ => MODPATH.’orm’, // Object Relationship Mapping
// ‘unittest’ => MODPATH.’unittest’, // Unit testing
// ‘userguide’ => MODPATH.’userguide’, // User guide and API documentation
));
[/php]

Create the [highlight type=”special_text”]kohana-blog/application/config/database.php[/highlight] configuration file under [highlight type=”special_text”]kohana-blog/application/config[/highlight] folder like as below. Change username & password accordingly.

[php highlight=”11,12″ title=”kohana-blog/application/config/database.php”]
<?php defined(‘SYSPATH’) or die(‘No direct access allowed.’);

return array
(
‘default’ => array
(
‘type’ => ‘mysql’,
‘connection’ => array(
‘hostname’ => ‘localhost’,
‘database’ => ‘kohana_blog’,
‘username’ => ‘root’,
‘password’ => ‘root’,
‘persistent’ => FALSE,
),
‘table_prefix’ => ”,
‘charset’ => ‘utf8’,
‘caching’ => FALSE,
‘profiling’ => TRUE,
)
);
[/php]

Congratulation!!! You are done with configuring your database.

Create a model

It is just easy. Create a [highlight type=”special_text”]article.php[/highlight] file under [highlight type=”special_text”]application/classes/model[/highlight] as shown below. Here, our [highlight type=”special_text”]Model_Article[/highlight] object extends [highlight type=”special_text”]ORM[/highlight]. By default it will assume table name from the model name (so cool). For article model table name is be articles. You can also change it, using [highlight type=”special_text”]$_table_name[/highlight] property. But this is not important now.

[php]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Model_Article extends ORM {

}
[/php]

This step is just easy, huh? We have few more steps to play with this article model.

Create the form to create/edit an article

Create a view file [highlight type=”special_text”]kohana-blog/application/views/article/edit.php[/highlight] under [highlight type=”special_text”]application/views[/highlight] folder as shown below.

[php htmlscript=”true” title=”kohana-blog/application/views/article/edit.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’); ?>

<h1>Create new article</h1>

<?php echo Form::open(‘article/post/’.$article->id); ?>
<?php echo Form::label("title", "Title"); ?>
<br />
<?php echo Form::input("title", $article->title); ?>
<br />
<br />
<?php echo Form::label("content", "Content"); ?>
<br />
<?php echo Form::textarea("content", $article->content); ?>
<br />
<br />
<?php echo Form::submit("submit", "Submit"); ?>
<?php echo Form::close(); ?>
[/php]

  • Create new action to load the form
  • Create post action to post & save the article

[php highlight=”11-19,21-29″ title=”kohana-blog/application/classes/controller/article.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Controller_Article extends Controller {

public function action_index() {
$view = new View(‘article/index’);

$this->response->body($view);
}

// loads the new article form
public function action_new() {
$article = new Model_Article();

$view = new View(‘article/edit’);
$view->set("article", $article);

$this->response->body($view);
}

// save the article
public function action_post() {
$article_id = $this->request->param(‘id’);
$article = new Model_Article($article_id);
$article->values($_POST); // populate $article object from $_POST array
$article->save(); // saves article to database

$this->redirect(‘index.php/article’); // redirects to article page after saving
}

}
[/php]

Now hit the url: http://localhost/kohana-blog/index.php/article/new and try creating some article.

Show list of articles

Lets improve our view to show all the articles.

  • Load all the articles. It is shown below changing article’s index action.

[php highlight=”5-11,12″ title=”kohana-blog/application/classes/controller/article.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Controller_Article extends Controller {

public function action_index() {
$articles = ORM::factory(‘article’)->find_all(); // loads all article object from table

$view = new View(‘article/index’);
$view->set("articles", $articles); // set "articles" object to view

$this->response->body($view);
}
….
[/php]

  • Change article/index.php as below to load all the articles.

[php htmlscript=”true” title=”kohana-blog/application/views/article/index.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’); ?>

<h1>Kohana Blog Homepage</h1>

<?php echo HTML::anchor("article/new", "New Article"); ?>

<?php foreach ($articles as $article) : ?>
<div style="background-color: #dddddd; margin: 0px 2px 0px 2px">
<h2><?php echo $article->title; ?></h2>
<pre><?php echo $article->content; ?></pre>
<?php echo HTML::anchor("article/edit/".$article->id, "Edit"); ?>
<?php echo HTML::anchor("article/delete/".$article->id, "Delete"); ?>
</div>
<?php endforeach; ?>
[/php]

Now hit the url: http://localhost/kohana-blog/index.php/article/index to see all created articles.

Update & delete an article

You can skip it, but this is to add the final touch of this part. Two more actions, edit & delete action, are added to the article controller.

[php title=”kohana-blog/application/classes/controller/article.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Controller_Article extends Controller {

const INDEX_PAGE = ‘index.php/article’;

public function action_index() {
$articles = ORM::factory(‘article’)->find_all(); // loads all article object from table

$view = new View(‘article/index’);
$view->set("articles", $articles); // set "articles" object to view

$this->response->body($view);
}

// loads the new article form
public function action_new() {
$article = new Model_Article();

$view = new View(‘article/edit’);
$view->set("article", $article);

$this->response->body($view);
}

// edit the article
public function action_edit() {
$article_id = $this->request->param(‘id’);
$article = new Model_Article($article_id);

$view = new View(‘article/edit’);
$view->set("article", $article);

$this->response->body($view);
}

// delete the article
public function action_delete() {
$article_id = $this->request->param(‘id’);
$article = new Model_Article($article_id);

$article->delete();
$this->redirect(self::INDEX_PAGE);
}

// save the article
public function action_post() {
$article_id = $this->request->param(‘id’);
$article = new Model_Article($article_id);
$article->values($_POST); // populate $article object from $_POST array
$article->save(); // saves article to database

$this->redirect(self::INDEX_PAGE);
}

}
[/php]

Now you can edit/delete an article also.

So this is it. I have tried to demonstrate the followings here

  • Configure an kohana application
  • Create a controller & action in kohana
  • Render view in kohana. Thogh this is pretty basic concept.
  • Working with ORM model
  • CRUD (create, read, update & delete) operation using Kohana ORM

Let me know what do u think. Any kind of suggestion is appreciable.


Downloads: https://bitbucket.org/kowsercse/kohana-blog
Part 2: Kohana Tutorial: Part 2

By | 2014-03-05T22:19:19+00:00 September 4th, 2011|kohana, php|93 Comments

About the Author:

2 hand 2 leg 2 ear and 2 eyes... i am a man who is living to live...

93 Comments

  1. maSnun September 4, 2011 at 8:01 PM - Reply

    Nice writeup bro, continue the series! 🙂

  2. Feroz September 4, 2011 at 8:40 PM - Reply

    Ah … what a great tutorial for the KOHANA beginners! I am so inspired that just jumping inside KOHANA with the help of this series 🙂

    • Kowser September 4, 2011 at 9:13 PM - Reply

      don’t just jump, bring the ladder too 😉

      • sai January 24, 2013 at 4:39 PM - Reply

        Can you get me example program of kohana using modules.

      • rama upadhya April 12, 2013 at 12:39 PM - Reply

        but actaully it was long jump not a high one……..:p

  3. Samiron Paul September 4, 2011 at 11:54 PM - Reply

    ya great step dude!! hope it will be really a helpful one.

    • rick March 26, 2013 at 4:57 PM - Reply

      if u any doubt means cantact me

  4. Miftah September 5, 2011 at 8:12 AM - Reply

    Its really helpful tutorial for beginners like me.

  5. Darren September 26, 2011 at 2:21 AM - Reply

    Cheers for the article,
    Perfect for a beginner like me.
    Look forward to more 🙂

  6. Abdullah Al Mamun September 29, 2011 at 2:00 AM - Reply

    This is one a great tutorial as a quick start. Hope, you’ll continue the series. Best of luck. 🙂

  7. 荒野无灯 October 5, 2011 at 8:42 AM - Reply

    good examples,thanks for your work.

  8. Ian November 2, 2011 at 6:58 AM - Reply

    Thank you for the great tutorial. I have been learning codeigniter over the past few weeks and having issues getting community written extensions not built in to work right and thought I’d give kohana a try since it is already set up for HMVC. I spent a couple of hours not finding any good tutorials for kohana and was about to give up hope until I found your site.

    Thank you very much.

    • Kowser November 2, 2011 at 8:30 AM - Reply

      Glad! that you found it helpful.
      I would love to write something on HMVC & Kohana.

  9. Nikolay November 8, 2011 at 2:10 PM - Reply

    Big thanks! I think it’s the best tutorial for Kohana beginners on the Internet. It’s easy to read, up-to-date and shows how to use ORM and Helpers. I learned a lot from this tutorial.

    However, I think some little improvements can be made (to make it even better):
    1. “Now remove/rename kohana-blog/installation.php” – install.php
    2. “Change kohana-blog/bootstrap.php as same as below” – kohana-blog/application/bootstrap.php
    3. If you rename “example.htaccess” to “.htaccess” you can access controllers as “kohana-blog/welcome” (not “kohana-blog/index.php/welcome”)
    4. There’s no need to specify the size of `id` INT while creating table (http://j.mp/t5HpzV). You should also make it UNSIGNED to double the maximum value (I personally think that MEDIUMINT UNSIGNED is enough in most cases)
    5. You can copy “database.php” from “modules\database\config\” and edit it (not create yourself)
    6. In action_post(): is it necessary to send $article_id to Model_Article()? Using debugger I found that “$article_id = null” while creating new article. I also made a test with “$article = new Model_Article();” and it worked.
    7. Please make a link to “Part 2” clickable 🙂 Thanks!

    • Nikolay November 9, 2011 at 2:40 AM - Reply

      Sorry, I was wrong with point 6. Function action_post() is completely okay. If you don’t pass $article_id you won’t be able to edit the article (it will add new articles instead of editing).

    • Kowser November 9, 2011 at 5:52 AM - Reply

      Glad to have a such a constructive feedback.

      I will do so as recommended and wish to inform you.

      Update: I have done it.

      • Tom April 6, 2012 at 9:53 PM - Reply

        Great tutorial, helped me a lot by getting started in MVC

        Thanks for posting this.

    • NOliver July 28, 2012 at 4:22 AM - Reply

      3) leaving .htaccess out of it might be useful for newbies by removing one more layer that can go wrong 😉 though you are absolutely correct

  10. […] Tutorial on kohana for the beginners.    PHP Read the original post on DZone… […]

  11. […] his blog today Kowser introduces you to Kohana, an easy to use, lightweight PHP HMVC framework that can help you get applications up and running […]

  12. SpadXIII November 11, 2011 at 2:00 PM - Reply

    Nice and easy tutorial. Just a note, you’re populating the article fields directly with $_POST. Might be a good idea to at least give a note that this is not the safest way of doing things. I haven’t read the other articles yet, so I’m not sure if you’re covering this already 🙂

    Best/better practice is to use Arr::extract to get just the fields you need out of the $_POST:

    $article->values(Arr::extract($_POST, array(‘title’, ‘content’)); // populate $article object from $_POST array

    instead of:
    $article->values($_POST); // populate $article object from $_POST array

    • Kowser November 20, 2011 at 12:54 AM - Reply

      Agreed and thanks a lot for your suggestion.

  13. Sinan E. November 23, 2011 at 8:47 PM - Reply

    I wrote my first module with this article.
    Passing to the second lesson…
    Thanks.

  14. Marco R. December 8, 2011 at 1:05 AM - Reply

    Hi all! Thanks for this great tutorial.
    Do you know a way to implement RSS feeds?
    Thanks!

    M.

  15. Kelly December 21, 2011 at 6:21 AM - Reply

    Hi Kowser,

    I am new to php and kohana. Got a probably pretty silly question – when I go to http://localhost/kohana_blog, the page shows hello, world, which is the function action_index in the Controller_Welcome class. The article.php also contains a action_index function in the same folder, why does the action_index in the Controller_Welcome class get executed and not the one from Article?

    Thanks!
    Kelly

  16. PHP by phrax - Pearltrees January 4, 2012 at 4:32 AM - Reply

    […] Kohana Tutorial: For the beginners | faded speech Kohana is the framework I liked best among other PHP framework. Its an easy one also . To me it is organized, highly object oriented, having clear separation of model, view & controller. Autoloading of classes is nice feature here also. […]

  17. gladys January 20, 2012 at 2:11 PM - Reply

    Hi all!
    First of all I would like to thank you for this good tutorial. However I encounter a problem where kohana framework add prefix to table name and prompt me error such as below:
    Database_Exception [ 1146 ]: Table ‘kohana_blog. article’ doesn’t exist [ SELECT ` article`.* FROM ` article` AS ` article` ]

    Hope anyone can help me on this. I’m figuring out why kohana add prefix which is single quote to table name, the table name appear as ‘article’ instead of article.

    • rami January 25, 2012 at 4:06 PM - Reply

      Hey man!
      Looks like there is a blank space ” ” before your table name. You may wanna check it in your configuration in /application/config/database.php. Table prefix should be ‘table_prefix’=>”

    • Kowser January 25, 2012 at 4:46 PM - Reply

      @gladys

      Sorry for being so late.
      Can you give me the full stack trace?
      You can also check @rami’s suggestion.

      Thanks for your feedback.

  18. rami January 25, 2012 at 11:59 PM - Reply

    Thanks a lot, man!
    I’ve got just a tiny little remark on the “Create a model” step above:
    The file mentioned – ‘article.php’ – should be in folder ‘/application/classes/model/’ not in ‘/application/model/’. At least that’s how it worked for me (version 3.2).

    • Kowser January 26, 2012 at 8:30 AM - Reply

      @rami: Updated. And many many thanks.

  19. wong January 26, 2012 at 10:17 AM - Reply

    thanks ….this is where i locate for

  20. gittirat February 18, 2012 at 7:45 PM - Reply

    Thanks…

  21. Php ve Framework Yapısı | PHP March 2, 2012 at 7:11 PM - Reply

    […] bir yorum yapamayacağım. Bu kaynaklarda daha detaylı bilgiye ulaşabilirsiniz : [1] – [2] – […]

  22. Jeremy March 10, 2012 at 2:16 PM - Reply

    Good write up though I am having a tiny issue. When editing an article a new article is being created instead of updating the selected article. I know that the article is being selected because the form populates all the fields. Any quick fix on why its creating new articles instead of modifying the current one?

    • Jeremy March 10, 2012 at 2:25 PM - Reply

      NM I figured it out. I forgot to add $player->id to the form method.
      I had Form:open(‘article/post’); instead of Form::open(‘article/post/’.$article->id);

  23. Adrian March 13, 2012 at 7:18 PM - Reply

    This is a very nice tutorial for a kohana beginer like me. I’ve also seen part 2 and it is very helpful and i’m wondering if you could help me with kohana 3.2 auth module. The documentation that I’ve found on other sites is not helpful at all. Thanks and have a nice day.

  24. Dipa March 28, 2012 at 12:32 PM - Reply

    Simply Awesome!

  25. NIKO'5 April 5, 2012 at 9:17 PM - Reply

    The best beginner practical tutorial!…you should make more publications..

  26. SivaM B April 17, 2012 at 12:57 PM - Reply

    Really Its an worth able tutorial for any developers!…Thanks a lot Kowser

  27. […] basic Tutorial Like this:LikeBe the first to like this […]

  28. SadanandGowda April 26, 2012 at 12:53 PM - Reply

    Thanks a lot Kowser, It really helped me a lot.

  29. Sreejith K May 1, 2012 at 2:04 PM - Reply

    Thanks Kowser, it really helped me to understand the framework.

  30. […] I’m running Kohana v3.2 I’m just trying to get up and running with a small MVC and was recommended this system. I’m following this tutorial: http://kowsercse.com/2011/09/04/kohana-tutorial-beginners/ […]

  31. JasonD June 18, 2012 at 7:55 PM - Reply

    Thank you for the very helpful tutorial, it was very useful and I learned a lot about Kohana.

  32. carlos June 19, 2012 at 1:40 AM - Reply

    I’m from Venezuela, and that makes it difficult to fully understand articles in another language than mine, but I assure you that of all the tutorials I’ve followed your beam was the best I mean even when in another language ..! helped me understand a lot of MVC, I hope you upload new tutorials of this MVC

  33. Dinesh July 16, 2012 at 4:53 PM - Reply

    Thanks a lot Kowser. This is one of the best tutorial available at present. Waiting for more posts!

  34. kourosh August 11, 2012 at 8:24 AM - Reply

    Greatfull Tutorial, tanx.
    but i have a question, i dont undrestand why we must passed the id as a parameter to
    the new action ? @ this moment we dont have any record in table, what this id specify exactly ?

    • Kowser August 14, 2012 at 10:18 PM - Reply

      Yes, fully agreed that the “id” was not needed to be passed while creating an article.

      I am just reusing the “edit.php” for for creating the creating. But surely it is not included with the post URL since “$article->id” is null.

      The good practice is to have different action+view for create & edit. I have created the action but not the view.

      Thanks for pointing it out.

  35. Sagar August 22, 2012 at 11:42 AM - Reply

    Nice Article. Very helpful.

    Thanks a lot.

  36. Larson September 16, 2012 at 11:40 PM - Reply

    Thank you for the very well written tutorial on the Kohana framework. I have dabbled with the framework in the past, but I never had a good grasp prior to your tutorial.

  37. Rocky September 17, 2012 at 5:48 PM - Reply

    Is it good practice to use ORM in controllers or should we use Model Class?
    e.g. $articles = ORM::factory(‘article’)->find_all(); in Controller_Article Above.

    Thanks
    RSVP

    • Kowser December 28, 2012 at 7:26 PM - Reply

      In my opinion, it is not a good practice to use ORM in controller. They should be hidden in service/business object call.

      controller->service->dao/orm.

      Consider above article just as a tutorial. Not to explain all these high level abstraction 🙂

  38. Sanyam September 18, 2012 at 11:00 AM - Reply

    Best article on Kohana available on net…
    keep up the good work…

    • Rocky September 28, 2012 at 3:18 PM - Reply

      Hi Sanyam,
      Hru? after long time..

      Hey could you please answer my about query if u kno kohana well?

  39. Tejinder Singh October 17, 2012 at 1:38 PM - Reply

    Great tutorial for beginners , thanks a lot.

  40. Shingi October 26, 2012 at 2:50 AM - Reply

    Hi guys, after posting a new article it is successfully saved to the database but when the application tries to redirect to the index page i get the following error:-

    ErrorException [ Fatal Error ]: Call to undefined method Request::redirect()

    Please help

  41. John December 16, 2012 at 10:49 PM - Reply

    As a retired, non-technical professor looking to learn something new, I found this tutorial to be outstanding. Any suggestions would be self-serving I am sure. Keep up the good work. I am looking forward to the 2nd tutorial.

    • Kowser December 28, 2012 at 7:27 PM - Reply
        I am honored. And thank you.
  42. Krzysiek December 23, 2012 at 7:40 PM - Reply

    Hi,
    I tried to replace some code to see what happens nad I was hoping to get the same results but I didnt.

    thats the factory method:

    public static function factory($model, $id = NULL)
    {
    // Set class name
    $model = ‘Model_’.$model;

    return new $model($id);
    }

    And…
    $articles = ORM::factory(‘article’)->find_all();
    I replaced with:
    $articles = new Model_Article();
    $articles->find_all();

    No error but I get no articles either, just “Kohana Blog Homepage” title and “New Article” link.

    What is diffrent?

  43. John December 27, 2012 at 4:48 PM - Reply

    When deleting an article, I get the following error.

    public function action_delete() {
    47 $article_id = $this->request->param(‘id’);
    48 $article = new Model_Article($article_id);
    49
    50 $article->delete();
    51 ****** $this->request->redirect(self::INDEX_PAGE); ******
    52 }
    53
    54 }

    note: asterisks are mine to indicate the line error

    Thank you for any help.

  44. John December 27, 2012 at 5:01 PM - Reply

    Problem solved changed error line to

    $this->request->redirect(‘index.php/article’);

    instead of

    $this->request->redirect(self::INDEX_PAGE);

  45. Tomo_2 January 7, 2013 at 7:47 PM - Reply

    Really nice tutorial – was looking it for a long time. Really nice job – ty for help 🙂

  46. Mans abbas January 14, 2013 at 2:38 PM - Reply

    ErrorException [ Fatal Error ]: Call to undefined method Request::redirect()

    why its mot redirecting to my article page.???

    • sara February 20, 2013 at 1:00 AM - Reply

      I had the same problem and I solved it by replacing>

      $this->request->redirect(self::INDEX_PAGE);

      with>

      HTTP::redirect(self::INDEX_PAGE);

      Hope it helps!

      • sara February 20, 2013 at 1:05 AM - Reply

        Or just:

        HTTP::redirect(‘article’);

        if not using index.php

      • Simon October 17, 2013 at 10:40 AM - Reply

        Sara, just an FYI, I found this works as well if you prefer to use the reference to the object method rather than the static method:

        $this->redirect(‘page’);

        Although, documentation on PHP suggests referring to the static method using “::” is more correct. Just thought I’d let you know.

    • Simon October 17, 2013 at 10:36 AM - Reply

      “$this->redirect(‘page’)” works with the new Kohana 3.3 as well. Just an FYI if you don’t want to use the HTTP static reference.

  47. Jouanne Jimmy January 27, 2013 at 4:17 AM - Reply

    Concerning the redirection, since the new update of Kohana, you need to use the new syntax :
    const INDEX_PAGE = ‘article’; // just give the name of Controller that we need it, otherwise the path will be : http://localhost/index.php/index.php/article
    $this->redirect(self::INDEX_PAGE);

    or just it if you don’t want to use a const :
    $this->request->redirect(‘article’);

    instead of

    $this->request->redirect(self::INDEX_PAGE);

  48. sara February 20, 2013 at 1:07 AM - Reply

    May God bless you for this really helpful tutorial!

  49. rick March 26, 2013 at 4:55 PM - Reply

    i want update program example for php

  50. George April 3, 2013 at 11:44 AM - Reply

    Nice tuttorial for bigginers just like me more of the same would really be appreciated if its okay maybe some attachments of the same etc. thankyou

  51. arumdev April 21, 2013 at 12:24 AM - Reply

    wow, this article was SOOO helpful. I’ve been developing basic websites with php for a few years now but I need something to allow good site templating and eventually a CMS etc. I decided to use kohana as the basis for all of this but had a hard time finding any tutorials and the few I did fine were out of date ( < 3.0 .

    Your tutorial has been great for me to understand the basics in practice (I understand the MVC concept but have never used it before so it was hard for me to know where to start).

    There were a few issues I had as I'm using 3.3 but in the end I got it all working and can now look at developing my first application with kohana, thanks to this page! so thank you very much.

  52. Kohana 3.3 Blog tutorial June 23, 2013 at 8:26 PM - Reply

    […] di tutto grazie a Kowser autore del bellissimo articolo link, tuttavia per kohana 3.3 e’ necessario apportare alcune modifiche che vi elenco di seguito. […]

  53. Moustafa Ashmawy July 14, 2013 at 9:12 PM - Reply

    Thanks for your effort but I’m facing some problem here.
    When i write
    http://localhost/kohana-blog
    http://localhost/kohana-blog/index.php/welcome
    http://localhost/kohana-blog/index.php/welcome/index
    in my browser it just shows
    ErrorException [ 8 ]: Array to string conversion ~ SYSPATH\classes\Kohana\Log\Writer.php [ 81 ]
    hope you can help

  54. vishalakshi September 5, 2013 at 5:01 PM - Reply

    hi Kowser,
    this is really nice tutorial. could you please give the examples of modules

  55. […] I am trying to setup my database config as demonstrated here: http://kowsercse.com/2011/09/04/kohana-tutorial-beginners/ […]

  56. Simon October 17, 2013 at 10:34 AM - Reply

    Kowser, thank you very much for this incredibly helpful start to Kohana! You have given me a basic introduction and helped me fall in love with the framework, all at the same time. The CRUD and ORM interfaces are awesome.

    I really appreciate your time and assistance!

  57. […] I am trying to setup my database config as demonstrated here: http://kowsercse.com/2011/09/04/kohana-tutorial-beginners/ […]

  58. Saibamen November 2, 2013 at 5:10 AM - Reply

    What version of Kohana is for this tutorial?

    • Kowser January 2, 2014 at 12:48 AM - Reply

      It is for Kohana 3.2.
      I have not updated the blog for a long long time.

  59. Baptiste January 14, 2014 at 6:32 PM - Reply

    Hi,

    Thanks for this helpful tuto,

    i just read there were some issues because of the version . Right but I think it could be interesting to mentioned it (for the others “like me 2 hours ago” …;-) )

    • database.php // “MySQL” and not “mysql” (because of casesensitive)
    in fact, take care about sensitivity everywhere (controler article -> ORm::factory (‘Articles’)

    • every where you use it : $this->redirect() replace $this->request->redirect().

    i hope i hav’nt forgotten any think.. thank’s gain your tuto helped me (a lot in fact), that’s why i will note there mentions.

    *******************************
    oh somethink else : don’t hesitate to lok at your logs folder (serverside of course)

    Baptiste

  60. Nadimuthu January 15, 2014 at 1:16 AM - Reply

    Nice post.. This is very helpful to learn something about framework.. And thanks once again…

  61. kodeeswaran January 20, 2014 at 11:15 AM - Reply

    i have downloaded the kohana-blog and implented in htdocs folder, now i am getting hello, world output, i couldnot execute the kohanablog, help what i have to do

  62. steven March 5, 2014 at 8:16 PM - Reply

    To get this to work on version 3.3.1, I changed
    $this->request->redirect(‘index.php/article’);
    in kohana-blog/application/classes/controller/article.php to
    $this->redirect(‘article/index’);

  63. steven March 5, 2014 at 8:30 PM - Reply

    I trained personnel for many, many years in a large organization. Most training manuals were not this simple, concise, and clear. I congratulate you on a very fine tutorial and very much look forward to working on Part 2.

    • Kowser March 5, 2014 at 10:13 PM - Reply

      Many many thanks for your feedback. I will use your suggestion to fix issues with the article.

  64. Kadyr July 13, 2014 at 8:23 PM - Reply

    This probably will be the dumbest question ever, but I’m brand new to Kohana, PHP, and web development in general.
    How exactly do you execute the .sql script?

  65. […] Cookies Kohana Tuts Kohana Installation Guide More Kohana […]

  66. firman June 18, 2015 at 3:12 PM - Reply

    please help me, all the scripts already equated from example

    error as follows===>>>

    Database_Exception [ 8192 ]: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

    MODPATH\database\classes\kohana\database\mysql.php [ 67 ]

    62 catch (Exception $e)
    63 {
    64 // No connection exists
    65 $this->_connection = NULL;
    66
    67 throw new Database_Exception(‘:error’,
    68 array(‘:error’ => $e->getMessage()),
    69 $e->getCode());
    70 }
    71
    72 // \xFF is a better delimiter, but the PHP driver uses underscore

    MODPATH\database\classes\kohana\database\mysql.php [ 171 ] » Kohana_Database_MySQL->connect()

    MODPATH\database\classes\kohana\database\mysql.php [ 360 ] » Kohana_Database_MySQL->query(arguments)

    MODPATH\orm\classes\kohana\orm.php [ 1504 ] » Kohana_Database_MySQL->list_columns(arguments)

    MODPATH\orm\classes\kohana\orm.php [ 392 ] » Kohana_ORM->list_columns(arguments)

    MODPATH\orm\classes\kohana\orm.php [ 337 ] » Kohana_ORM->reload_columns()

    MODPATH\orm\classes\kohana\orm.php [ 246 ] » Kohana_ORM->_initialize()

    MODPATH\orm\classes\kohana\orm.php [ 37 ] » Kohana_ORM->__construct(arguments)

    APPPATH\classes\controller\article.php [ 11 ] » Kohana_ORM::factory(arguments)

    {PHP internal call} » Controller_Article->action_index()

    SYSPATH\classes\kohana\request\client\internal.php [ 118 ] » ReflectionMethod->invoke(arguments)

    SYSPATH\classes\kohana\request\client.php [ 64 ] » Kohana_Request_Client_Internal->execute_request(arguments)

    SYSPATH\classes\kohana\request.php [ 1138 ] » Kohana_Request_Client->execute(arguments)

    DOCROOT\index.php [ 103 ] » Kohana_Request->execute()

Leave a Reply