Junal on the run

Similarities and dissimilarities between CakePHP & CodeIgniter (Part-I)

Posted on: December 14, 2007

Both of these PHP Frameworks follow model-view-controller architecture. This is why both of them got lots of things in common but at the same time they have some dissimilarity too. I was thinking to make a list of all those similarities and dissimilarities with some small examples so that when I move one framework to another, it can remind me the dissimilarities. I have pointed out some main features of these 2 frameworks to discuss about.

Installation: well, both of them got similar installment process. Like CodeIgniter installation in 4 steps.

  1. Unzip the package.
  2. Upload the CodeIgniter folders and files to your server. Normally the index.php file will be at your root.
  3. Open the application/config/config.php file with a text editor and set your base URL.
  4. If you intend to use a database, open the application/config/database.php file with a text editor and set your database settings.

This installation process is same for Cake “development setup”. But we don’t have to setup any URL for Cake.

Dissimilarities come when we want to increase security hiding our location. CI’s system folder can be renamed into something private. But in this case $system_folder variable name has to be changed from inex.php file into chosen name of the folder. CakePHP has 2 more installation process, these are: using a development setup, which allows us to easily view many CakePHP applications under a single domain, or using the production setup, which allows for a single CakePHP application on the domain.

Although both of their installations are very easy but comparing these two, I guess CI’s installation process is easier.

Scaffolding: I wrote a blog on Cake Scaffolding and CodieIgniter Scaffolding. I’m gonna say the same thing here, so let me just paste it here:

In cake Once you add this variable called $scaffold in your controller it gives an idea about early parts of development of your application.

Example: <?php

class YourController extends AppController

{

var $scaffold;

}

?>

But what if you forget to take it off before you up the project in real server and somebody guesses your URL? Well certainly scaffolding has edit and delete option right. So it’s not secured I guess.

I like how CodeIgniter is dealing with scaffolding. Almost same but there is an option for you to set secured scaffolding. You have a secret word option which will be set here:

application/config/routes.php in CI like this

$route [‘scaffolding_trigger’] = ‘ sewt ‘; //assume your secret word is “sewt”

So now use your scaffolding in the controller

<?php
class YourController extends Controller {

function Test()
{
parent::Controller();

$this->load->scaffolding(‘table_name’);
}
}
?>

To see this scaffolding you have to follow this URL path:

http://www.your-site.com/index.php/YourController/sewt/.

Isn’t it better eh?

Default Layout: CakePHP uses default layout, the layout has two variables by default: title_for_layout and content_for_layout. Each action automatically links to a particular view which gets spat into place. This is simply great to use for many pages. Once default page is setup, boring stuff like adding header and footer again and again completely gone.

CodeIgniter doesn’t have any default layout! I feel this is bad side of CI that it doesn’t have any default layout like Cake. It’s like row php code where I have to include header, footer each and every time I create a new file. To help on it I guess this post is really helpful, have a look here: http://hasin.wordpress.com/2007/03/05/adding-yield-codeigniter/#comment-55979

Model: first of all, I would like to give an example of a CakePHP model.

<?php
class User extends AppModel
{
var $name = ‘User’;
var $hasOne = array(‘Profile’ =>
array(‘className’ => ‘Profile’,
‘conditions’ => ”,
‘order’ => ”,
‘dependent’ => true,
‘foreignKey’ => ‘user_id’
)
);
}
?>

Here, Variable name (‘User’) must start with Capital letter and table name must be plural (‘users’). This is a strict convention that Cake follows.

One of the most powerful features of CakePHP is the relational mapping provided by the model. In CakePHP, the links between tables are handled through associations. Associations are the glue between related logical units.

There are four types of associations in CakePHP:

1. hasOne

2. hasMany

3. belongsTo

4. hasAndBelongsToMany

When associations between models have been defined, Cake will automatically fetch models related to the model you are working with. For example, if a Post model is related to an Author model using a hasMany association, making a call to $this->Post->findAll() in a controller will fetch Post records, as well as all the Author records they are related to.

In codeIgniter there are no model associations. There is no strict naming convention. Although class name must be capitalized. Let see an example if CodeIgniter model:

class Blogmodel extends Model {

var $title = '';
var $content = '';
var $date = '';

function Blogmodel()
{
// Call the Model constructor
parent::Model();
}

function get_last_ten_entries()
{
$query = $this->db->get('entries', 10);
return $query->result();
}

}

So, query in CI is almost like writing SQL command. I feel this is better in a sense that my query is optimized here. It searches exactly what I need. In Model association concept I saw many unnecessary results that I didn’t ask for. But sometimes I feel I can call CodeIgniter as VC pattern framework, because I can do almost  the same work of model in Controller🙂

Ok, that’s all for today ….if you have any complains/comments/suggestions please write it down. I’m coming up with more similarities and dissimilarities in my next post……

References:

http://codeigniter.com/user_guide/

http://manual.cakephp.org/

4 Responses to "Similarities and dissimilarities between CakePHP & CodeIgniter (Part-I)"

CakePHP has DS (Directory Separator) variable inside its config file, but codeigniter doesn’t have any. Do you think this could create any problem on Windows Server with IIS running on it?

@Usman : It might…

@Usama Ahmed and @junal,

I was able to run CodeIgniter 1.7 successfully over IIS7, Windows 2008 Web Server box. Using fastcgi and SQL Server 2005 (with SQL Server 2005 PHP Driver).

@julian
can you explain what exactly you did to make the driver running. I’ve been searching like crazy how to connect CI to SQL Server 2005. Which driver did you mean on your comment above?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

View Junal Rahman's profile on LinkedIn
Subscribe to me on FriendFeed

Follow Me on Twitter

Archives

My photos

Downtown, Ottawa

Dream

Jasper, AB

Jasper, AB

Banff, AB

Folkfest, Bangladesh Pavilion Saskatoon

Lovely Sky

Stunt bikers on summer festival, Saskatoon

More Photos

Blog Stats

  • 420,271 hits
%d bloggers like this: