(note (code cslai))

MVC: Kohana vs. Zend Framework

After comparing my own implementation of MVC with CodeIgniter’s, now I’m comparing Kohana’s and Zend’s. I have just shifted from CodeIgniter to Kohana recently in work and is currently learning on how to use Zend Framework to build my web-app. As everybody knows, Zend Framework is more like a collection of library classes than a framework a la Ruby on Rails, using MVC in Zend Framework would require one to begin from bootstrapping stage. However, in Kohana, just like other frameworks, bootstrapping is done by the framework itself so the developer will get an installation that almost just works (after a little bit of configuration).

Controller

Basically there is no much difference in the controller, besides the naming convention that one has to follow. However, in Zend Framework, every public action method in the controller will be configured to a view template by default. For example, if you have a controller as follows:


<?php
class IndexController extends Zend_Controller {
public function __construct() {
parent::__construct();
}
 
public function indexAction() {
// some code
}
}

Even if you choose only to perform one print hello world statement, you will still get an error message indicating that the view is not found, as follows:

Uncaught exception ‘Zend_View_Exception’ with message ‘script ‘index/index.phtml’ not found

Just like what I mentioned above, in Kohana, they don’t even care whether you are using view, I can simply just output from the controller whenever I want.

View

From the first glance, Kohana and Zend Framework is doing similar things with the View. In Kohana, you can have a view defined with the following code in the controller (or any other place):


// in the controller
// instantiate the view object
$view = new View('path_to_the_filename_without_extension');
// another form
$view = new View;
$view->set_filename('path_to_the_filename_without_extension');
// yet another form
$view = View::factory('path_to_the_filename_without_extension');
 
// inject in some values to the view to be manipulated
$view->set('variable_name', 'some value');
// this is also valid
$view->variable_name = 'some value';
 
// display the view
$view->render(TRUE);

Most of the time you won’t even need to further subclass the supplied View class. To display content to the browse, you will need a view file (more like a template file). The view file will be something like follows:


<html>
<head>
</head>
<body>
<p><?php echo $variable_name; ?></p>
</body>
</html>

In Zend Framework, the code is almost similar, but I think they have more to offer if you want flexibility.

Model

In Kohana, you have 2 choices, either to use ORM as model or just a model that doesn’t really do anything much by default. I use a variant of IgnitedRecord as model in work, and I just started to question whether ActiveRecord pattern is suitable as a model in MVC few weeks ago. In Zend Framework you get no model at all as you are expected to create one for yourself.

Conclusion

Nothing much to conclude, but through the comparison between 4 different implementations to MVC, I kinda learn a lot. As I have just only started learning MVC these days, I think I will post further notes on MVC in Zend Framework soon.

Exit mobile version