My First Kohana App
I have been looking through the core of Kohana v3 lately, and there is a lot of good stuff in there. I decided to port an application from CodeIgniter to Kohana to test the waters. Here is my story and opinion on Kohana development.
Obviously with any framework, there is a learning curve. CodeIgniter has a very small learning curve because of their excellent docs. However, Kohana has HORRIBLE docs. They do have a very nice API though which helps a lot. I spent some time looking through the source to find out how a few things worked. Personally, I don’t mind this, I like knowing exactly how it’s working in the backend.
The application I was porting is http://9src.com, a URL shortening service.
Configuration
All (most anyway) of your configuration will be done in the application/bootstrap.php file. It is very well documented. However, one very important thing to note is that for production sites, you will want to turn off profiling (which is on by default). It is in the Kohana::init call. There are very good comments, so you should figure that out very easily.
Routes
So the first thing I was worried about was routes. Here are what my routes in CodeIgniter looked like:
$route['api'] = 'api';
$route['(:any)'] = 'short/route/$1';
Here is what they look like in Kohana:
Route::set('api', 'api')->defaults(array(
'controller' => 'public',
'action' => 'index',
));
Route::set('default', '')->defaults(array(
'controller' => 'public',
'action' => 'index',
));
Route::set('route', '', array('code' => '.*'))->defaults(array(
'controller' => 'public',
'action' => 'route',
));
The Kohana’s routes require more code, but they are also much more powerful than the CodeIgniter ones. I won’t go into full details here, but if you search around you can find more info on the routes.
Controllers
Controllers in Kohana have a lot of cool features that CodeIgniter ones do not. Here are some of the features in Kohana Controllers:
- before() function - gets ran before the action (method) gets called.
- after() function - gets ran after the action (method) but before the output.
- Controller_Template class - You can extend this class and it will automatically load a template view and render it.
- REST_Controller class - You can extend this class and create RESTful controllers.
I chose to extend the Controller_Template class because I have 1 main template I use. So I just had to name my view file “template.php” and stick it in the application/views folder. Then, for the partials I just set a template variable to the view. Here is my action_index() function (NOTE: all methods that can be called by URL are prefixed with “action_”):
class Controller_Public extends Controller_Template {
public function action_index()
{
$long_url = isset($_POST['long_url']) ? $_POST['long_url'] : NULL;
if(!empty($long_url))
{
$data['url_info'] = Shortener::url($long_url);
$this->template->body = new View('partials/created', $data);
}
else
{
$this->template->body = new View('partials/form');
}
}
}
In the above code you notice a few things. The first thing is that you set template variables by doing a $this->template->variable_name. Then, $variable_name is available in the template.
Next you will notice that you can load views into the template from the controller by setting a variable to a new View object. Then, in the view you simply echo the variable.
Models
Models in Kohana can extend either the base model, or a few other libraries. I chose to use the ORM class. It is SUPER simple to use for basic usage. It is also VERY powerful for more advanced operations. Here is my Url Model:
class Model_Url extends ORM
{
protected $_db = 'default';
}
Thats it! Here is an example function from my Shortener library which uses the Model:
public static function get_url($code)
{
$url = ORM::factory('url');
$url->where('code', '=', $code);
if($url->reset(FALSE)->count_all() == 0)
{
return FALSE;
}
return $url->find();
}
If you are familiar with ORM at all it pretty much makes sense.
Conclusion
Although it does have a bit of a learning curve (because you will spend time in the code), it is very simple and powerful. I have obviously left out some things like some configuration and such. However, the code is VERY well commented, and you should be able to figure out the configuration pretty easily.
The Unofficial Kohana 3.0 Wiki will help you out A LOT as you start using Kohana: http://kerkness.ca/wiki/doku.php
I won’t be switching from CodeIgniter to Kohana fully any time soon, but I will definitely be create most of me new apps in Kohana.
Don’t worry, I will still be putting out Kick-Ass CodeIgniter libraries too.
Comments
There are no comments for this yet. Be the First!