Junal on the run

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

Posted on: December 20, 2007

Okay, in my last post I stopped with model. Today, I’m going to start with Controller.

Controller: There is no significant difference between these framework controllers. Cake follows strict naming convention. So that once I follow the naming convention, I don’t have to worry about adding my model in controller. Let me make it clear. In my model when I define my model name like this:

$var name = ‘Post’;

I don’t have to add this Model name to get this table value in my post_controller.php. But if I want to add more models then of course we have to follow this:

var $uses = array(Post’,’AnotherModel’);

Cake controllers feature a number of callbacks you can use to insert logic before or after important controller functions. To utilize this functionality, declare these functions in your controller using the parameters and return values detailed here.

beforeFilter( );  
  ;

Called before every controller action. A useful place to check for active sessions and check roles.

afterFilter( );  
  ;

Called after every controller action.

beforeRender( );  
  ;

Called after controller logic, and just before a view is rendered.

On the other hand CodeIgniter doesn’t have any Callback function like cake. It doesn’t have any model name convention. So every time I have to add or load my model in my controller. Same goes when I add helper.

$this->load->model ('Model_name1', 'Model_name2'...);

$this->Model_name->function ();

I like how cake lets us assign the model name and later we can use it in controller. But we have a solution to do the similar thing in CI.

We can assign the model name in a variable like this:

$this->load->model ('Model_name', 'fubar');

So it makes thing a little easier I guess.

Oh well, as I said earlier cake follows naming convention, so when I have a function in controller called function test(). My view test.ctp will get it automatically. So when I call the controller mysite/controller/test it will automatically get the view. But in CI I have to tell the function which view to load when I call the function. Something like this:

<?php

class Blog extends Controller {

function index()

{

$this->load->view (‘blogview’);

}

}

?>

Okay, for time being I’m liking Cake’s aoutomagic features🙂

Helpers: Uff, CI doesn’t have any AJAX helper!! It has some different and helpful helpers comparing Cake, such as Date Helper, Download Helper, Cookie Helper any many more but man where is AJAX helper??! I prefer cake’s helper because I will get really a good support to use AJAX in my application. Well, I have got an alternative solution to use AJAX helper in CI. Have a look here:

http://codeigniter.com/wiki/AJAX_for_CodeIgniter/

In cake some helpers that are not default, have to be added. On the other hand in CI if we think we will use some helpers almost in all controllers than we can use Auto-loading Helpers.

application/config/autoload.php

Example:

$autoload[‘helper’] = array(‘url’, ‘file’);

Validation: Again, I should call CI as VC patter framework🙂. CakePHP handles its validation through the model itself. So I have ” Post” model and let say it has 3 fields called Name, Address and phone. To validate these fields I have to validate them in the model called Post. But in CI there is no restriction. I can do it in Controller. Well, that’s not a major factor. I find it easier but it just makes my controller size bigger [ugly eh?].

An example of Cake’s Validation:

var $validate = array(

‘login’ => ‘/[a-z0-9\_\-]{3,}$/i’,

‘password’ => VALID_NOT_EMPTY,

’email’ => VALID_EMAIL,

‘born’ => VALID_NUMBER

);

Well it uses variable $validate which is okay. But I prefer to use custom validation to use beforeSave() in my controller. And well, Cake 1.2 has a significant improvement over Validation. Its added more built in validation roles which are great. Now it supports alphanumeric, email, maxlenght, url and phone validations.

More about Cake validations are here: http://cakebaker.42dh.com/2007/01/03/validation-with-cakephp-12/

Data validation in CodeIgniter is handled via a validation class. A set of rules get defined and assigned to the validation object. The validation object automatically validates the data passed via the URL or form. From there, you can decide how that gets handled. The validation class can also help automate some of the process of setting error messages for specific fields.

Documentation: CodeIgniter is well documented. This is why I always tell a starter to start with CI. CI also has forums and a wiki which feature a lot of user-submitted code.

On the other hand Cake’s manual is not that strong against CI’s documentation. Manual says enough but I don’t think it’s enough for a newbie to get familiar with all sort of things that cake’s API says.

Well, this is where my post is finished. I know all these things that I have said not enough to find all similarities and dissimilarities of Cake and CI. So I would like to ask you to correct me if I made any mistakes or understood anything wrong according to my description. You can tell me to add more in the post. Your any comment will be welcomed.

Thanks a lot🙂

References:

http://codeigniter.com/user_guide/

http://manual.cakephp.org/

http://snook.ca/archives/php/codeigniter_vs_cakephp/

1 Response to "Similarities and dissimilarities between CakePHP & CodeIgniter(Part-II)"

Nothing like what I initiate it windscreen to hamstring like before, but one fifth david beckham naked photos we did have was that we did mindlessly reverse in shield before the hull of fourteen.

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: