How is Kohana different from CodeIgniter? [closed]
Asked Answered
D

2

24

I've been using CodeIgniter for a long time, however lately I've been feeling the need to move to a more advanced/more OOP framework. Kohana seems to be an often recommended option, my question is, how exactly is Kohana different from CodeIgniter? A list of differences, particularly syntax differences, would be great.

Denominationalism answered 17/4, 2011 at 11:8 Comment(7)
I feel the same way. CI is fine and all but at the end of the day you end up doing 90% of the work yourself. I am up to my armpits in extended and overloaded CI classes. I considered Kohana but it seems to be a moving target: always some major non backwards compatible changes in new versions. I still have yet to spend more than a few hours with it. I know that Kohana uses the Kohana::class syntax as opposed to CI's $this->classPump
Related, but didn't seem to get much attention: http://stackoverflow.com/questions/5559549/kohana-vs-codeigniter-year-2011Pump
You might also be interested in Fuel PHP fuelphp.com , made in part by CI developersJanayjanaya
Actually if you are framework shopping, I have heard good things about Symfony. I have not tried Fuelphp but I know its developers are very motivated. From what little I've seen, it appears to be very similar to Kohana, but keep in mind it is php5.3 only. (Note to self: don't turn this into a framework summary, respect the OPs question) :)Pump
Madmartigan: "I am up to my armpits in extended and overloaded CI classes" Sounds like you could make some great pull requests to CodeInigter Reactor which would make everyones life better. As for "but keep in mind it is php5.3 only" not much of an issue now that PHP 5.2.x is no longer supported by PHP/Zend. Time to move on.Geosyncline
@Phil Sturgeon You're probably right that 5.3 is not really an issue these days. I'm still on 5.2 however (work server). I had to pull teeth just to get ftps access and phpmyadmin installed. I'd assume there are others like me who are "stuck" on 5.2 for now. As far as Reactor goes sounds good - I will see what I can provide.Pump
I retagged the topic to be more specific about versionsIa
I
45

I'll be writing about Kohana 3.1 and main advantages I've seen over CodeIgniter 2 so far. Before Kohana 3.0 (year and a half ago), I used CodeIgniter, ZF, Symfony, Cake and tried many others (at the moment trying to do Yii only because I have to). I'm aware that many will spit on me for "being subjective". Go ahead.

Strict PHP 5.2

Kohana doesn't use any of the old code (meaning 2.x or CodeIgniter's) in the current version. It was totally rewritten to be fully object oriented and not to get in developer's way. Simply put, it feels natural to develop with it, like it's "the way" for PHP development.

This is a framework built "by the community, for the community", not for promotional purposes. And not by / for any community but a very leet one.

CodeIgniter stood still for way too long hanging on PHP4. Looking at the CI2s' source, I can't say they've fully moved on to PHP5 (let's say PHP 5.1 isn't really ... PHP 5). I've seen FuelPHP, which seems to be more like a mashup of CI2 with Kohana than a CI2 branch, though I have to say it definitely has potential.

HMVC

The main reason for Kohana to feel natural was born out of this pattern. The idea was to isolate every request to respect the pattern and it ended up with respecting the RFC 2616. Now you have a separate Response object for each Request, being able to reuse your code in a really neat way. Right now I'm working on a web service which is used with iPhone, Android and the web app. I can't describe what a privilege it is to call the API "on the inside". Raw example:

public function action_delete()
{
    $deleted = Request::factory('api/route')
        ->method(Request::DELETE)
        ->headers('Accept', 'text/html')
        ->execute();

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

No handicaps

The team behind Kohana is dedicated to making the framework "the best it can be", not "everything it can be, because we .. have too much spare time". Each maintenance version is backward compatible with previous ones (e.g. 3.1.2 with 3.1.0), having all "major" changes wait for the minor versions (e.g. 3.1 isn't fully backward compatible with 3.0 "out of the box"). Previous minor versions are maintained for 6 months after the new one is released.

Exten(ding|sions)

Kohana's Cascading File System makes it extremely easy to extend existing components, or even the ones used by vendors. You can override everything on application / module level, without thinking about setting include / load paths manually anywhere (because all files and folders respect the same convention).

There is a whole bunch of modules, including Zend Framework. How's that? Simply put, you can even use ZF or any other library as a module inside of your Kohana app.

Besides all the community built modules, every Kohana installation includes a few which almost every application can benefit from:

  • Auth A simple, yet very powerful, authentication library. The module itself provides file auth driver only but enabling ORM gives us a more powerful one.

  • Cache Caching library with drivers for most popular caching techniques: APC, eAccelerator, file, memcache, SQLite, Wincache and Xcache. It is extremely easy to implement and change (even later changes of the cache driver are a one-liner).

  • Codebench If you need to benchmark some code, Codebench provides you a very simple way of doing it.

  • Database As everything else in Kohana, database module is also fully object-oriented and extendable. Comes with full MySQL and PDO support.

  • Image A simple module for easy image manipulation

  • ORM Default ORM based on ActiveRecord pattern, fully using the advantages of the Database module combined with PHP 5 magic methods. Besides this one you can use Jelly, Sprig, AutoModeler or any other custom PHP lib like Doctrine.

  • Unittest Kohana comes pre-packed with a great unit testing module. It's based on PHPUnit and fully "integrates" with your app, allowing you very easy TDD. Besides, the whole framework is unit tested.

  • Userguide Altough ignored by most developers, this module is one of the most powerful Kohanas' features. It provides the easiest possible way of tracking your API and the rest of Kohana documenation. Why wouldn't your application have its' own guide as well? You don't even have to think about it! As long as you keep track of comments / conventions in your code, this module will take care of the rest.

Code examples

Code is neater than CI's, having all classes autoloaded, though conventions are very similar.

Update example (using ORM):

public function action_update($post_id)
{
    $post   = ORM::factory('post', $post_id);
    $errors = array();

    if ($values = $this->request->post())
    {
        try
        {
            $post->values($values)->update();

            $this->request->redirect('post');
        }
        catch (ORM_Validation_Exception $e)
        {
            $errors += $e->errors();
        }
    }

    $this->template->content = View::factory('post/update', array(
        'post'  => $post,
        'errors'=> $errors,
    ));
}

In this example ORM is used to update a row, having update() call it's check() method to validate it's values. In case that validation fails, ORM_Validation_Exception is caught and the rest of try block isn't executed (i.e. redirection to /post). In the end, both the post object (Model_Post) and errors array are passed to View where they can be accessed directly.

Notice that all Database_Query_Builder methods are available inside of ORM, so you can also do 'fancy' stuff like:

ORM::factory('post')
    ->where('user_id','=',$user_id)
    ->join('users','INNER')
    ->on('users.id','=','posts.user_id')
    ->find_all();

Or (with relations):

$user = ORM::factory('user', $id);

foreach ($user->posts->find_all() as $post)
{
    foreach ($post->quotes->find_all() as $quote)
    {
        if ($quote->illegal())
        {
            $quote->delete();
        }
    }
}

Where illegal() can be some custom model-level method with fancy joins and stuff. I'm aware how inefficient this chunk looks like, it's just a code example, not "are JOINs better than additional queries" :)

Ia answered 17/4, 2011 at 17:24 Comment(4)
+1 overall. I'm not familiar with Kohana and found this quite useful. Just a quick note though, typically versions are major.minor.maintenance. You appear to be referring to maintenance versions are backwards compatible and minor versions are not guaranteed to be. A major version change would be from 2.3 to 3, for example.Kristankriste
@coreyward: thanks, I referred to minor ones as "major" and maintenance ones as "minor" because most of the community refers to them that way. This is the second "major major version" I'm talking about anyways :)Ia
Great answer :). Its a bit worry that every major release breaks the previous versions.. Can you give some details on how things like the controllers, models, views work differently than CI, and perhaps also how form validation and DB access/queries are done?Denominationalism
@Click Upvote: I updated the post with a few small code examplesIa
S
0

Please have a look at https://stackoverflow.com/questions/717836/kohana-or-codeigniter where the link - http://onwired.com/blog/exploring-kohana-as-an-alternative-to-codeigniter/ - answered most of this very same question to me.

Skinner answered 17/4, 2011 at 11:12 Comment(6)
Isn't this possibly a bit dated? CI and Kohana have both released major versions since September '08. It would be nice to hear some feedback that is a bit more fresh and relevant. For instance, both articles cite the difference that Kohana is php5, when in fact they are both php 5 now. Now at version 2.2, Kohana is ready to enter the ring and compete for our attention... Kohana is at version 3.12 now :(Pump
Just outlined some differences for me when I was interested in switching a couple of years ago, but as I'm still happy with Codeigniter I really can't give more than the links I found when I was asking the same question.Skinner
I agree its dated.. would be nice to hear from a current user of Kohana. I might put a bounty on this question for a good answer.Denominationalism
@Click Upvote: Have you tried installing Kohana yet?Pump
@Mad No, I want to see if I want to use it before I install it.Denominationalism
Just install it and have a play. Nobody can tell you what is best for you.Geosyncline

© 2022 - 2024 — McMap. All rights reserved.