2 Nov
10 Days Later: Early Impressions of CakePHP vs. CodeIgniter
As loyal readers to my blog know, my employer has committed to using CodeIgniter for projects, although I hold out hope that new projects with my beloved CakePHP. That's okay, I'm a big boy and can handle working with different things as clients (and bosses) demand. So, after maybe a week and a bit of working with CodeIgniter I thought I'd talk about it.
Once you know one MVC framework, you know them all. My experience with CakePHP and Zend Framework means that understanding how CodeIgniter works in the context of MVC is simple.
CodeIgniter mixes OOP and functions. CakePHP has a much, much stronger commitment to using objects to access core functionality. A quick example is this is the way both frameworks handle forms in the views.
But within the controllers you are calling methods of objects to do things.
-
$data = $this->defaultdata();
-
$this->load->model('Leagues');
-
$lookupResults = $this->Leagues->getLeagues();
-
$data['lookupResults'] = $lookupResults;
-
$data['mainview'] = 'league_browse';
-
$this->load->vars($data);
-
$this->load->view($data['layout']);
Now, take this with a grain of salt because I am inheriting other people's code here so this might not be 'CodeIgniter best practices' but I think a little consistency would go a long way in the decision to use either functions or objects.
Lack of conventions makes Chris sad. I am very used to CakePHP's habit of just knowing where things are supposed to be instead of me having to specify everything. Yes, I understand that CodeIgniter is all about doing it your way, but I've grown to like the CakePHP way. I like that my actions in the controller know where to find the view...as long as I've followed the conventions.
CodeIgniter's User Guide Is Awesome. Logical groupings, cool Web 2.0 features when you click on the table of contents. Yes, we finally have some documentation for CakePHP 1.2 available but it is 'alpha with extreme prejudice' in it's current form. I'm not saying CakePHP should copy what CodeIgniter is done. I'm sure there will be an awesome final place for the CakePHP 1.2 documentation. But for someone starting out with CodeIgniter, the user guide is an essential tool.
No built-in associative data mapping makes Chris sad. But then again, I'm not really having to deal with much in the way of databases on this current project due to us using eXist.
So, there you have it. A very, very early look at my thoughts on CakePHP vs. CodeIgniter. I'll post the more interesting things I come across along the way as my current project gets built out more...
Article Tags >> CakePHP || codeigniter
Posted by nate on 02.11.07 at 4:46 pm
It's $form->select(), not $form->dropdown().
Posted by Chris Hartjes on 02.11.07 at 4:46 pm
@nate
Corrected...I did the post really quickly
Posted by developercast.com » Chris Hartjes’ Blog: 10 Days Later - Early Impressions of CakePHP vs. CodeIgniter on 02.11.07 at 4:46 pm
[...] Hartjes, after spending ten days (a week-ish) working with CodeIgniter, has posted some of his thoughts comparing the framework to one he likes just a bit more, CakePHP. As loyal readers to my blog [...]
Posted by Nate Klaiber on 02.11.07 at 4:46 pm
Nice overview! I would agree with you. When I was seeking out a good PHP Framework, I simply fell in love with CakePHP. Not that CodeIgniter, Symfony, or Zend were bad - I just loved the ease that came with CakePHP. And, it still lets you do what you need - you have full control to overwrite/extend methods.
One of my biggest reasons for choosing Cake was its ease of installing and getting up and running/developing.
Posted by Thijs on 02.11.07 at 4:46 pm
CodeIgniter might be a cool framework, but I haven't mapped http://codeigniter/ to the actual web site in my hosts-file so it's not working
Posted by Chris Hartjes on 02.11.07 at 4:46 pm
@Thijs
Thanks for pointing that out...fixed it in the post.
Posted by Mahmudul Hasan on 02.11.07 at 4:46 pm
But CodeIgniter works with both Php4 and php5, and with both IIS and Apache. And who says CodeIgniter is hard to install ?
Anyways, I too believe that Cake is a stronger framework. But we were forced to use CodeIgniter, because back that day, we had to build some modules for a web site which was already hosted on an IIS server and supporting only php4.
Posted by Skateinmars on 02.11.07 at 4:46 pm
Nice post.
But (as a codeigniter user) I don't think simple functions are bads when they are used as helpers. The functions you pointed out are only intended to be used in views and in this place it is really more conveniant than creating an object (especially for functions like base_url)
Posted by Chris Hartjes on 02.11.07 at 4:46 pm
@Skateinmars
It's more about consitency than anything else. I believe you should pick one method and go with it. Mixing leads to sloppiness, if you ask me.
Posted by Chris Domigan on 02.11.07 at 4:46 pm
I actually switched from CakePHP to CodeIgnitor for the following reasons:
* As great as the auto-association stuff that Cake does for you is, my app is complex enough that 9 times out of 10 I needed to write dedicated queries to give me the needed flexibility and speed. The overhead Cake introduced in its associations was beginning to slow things down.
* I don't generate HTML views for my pages, just send JSON data to a JS frontend, so I didn't need all Cake's advanced View stuff (and convention that "required" you to always have a separate view file). I appreciated the flexibility of the "looser" MVC approach in CodeIgnitor.
* The number of really useful utilities (helpers, classes etc) built in to CodeIgnitor was very impressive.
* Small footprint
* Great documentation.
I thoroughly enjoyed my time working with Cake, but I found CodeIgnitor offered me the speed and flexibility I needed for this particular app.
Posted by Michael Wales on 02.11.07 at 4:46 pm
The discrepancy between functions utilized in the View files whereas methods are used in the Controllers can be a blessing in disguise.
Functions allow non-programmers to flex the Views as much as they like. Take, for instance, Wordpress. There are literally thousands of people out there that can't write PHP code for the life of them yet they develop Wordpress themes. The functions provide a sense of simplicity, whereas OOP practices can tend to complicate things unnecessarily. In addition, the functions are merely wrappers for methods within the class (I believe - I'd have to look over the source again) - so if you truly wanted to go OOP, it shouldn't be an issue.
The sloppy code you mention in the Controller is just that - sloppy code. The developer is truly abusing the flexibility within CodeIgniter (and the fact that it runs on PHP4 or 5). Honestly, I don't even think that is valid code.
Personally, I always use CodeIgniter on a PHP5 installation and write my code as such. You can pass the $data variable to the View as an object just as easily as it can be an array.
// Any loading should be at the top of the method
$this->load->model('users_m');
$this->load->model('layout_m');
// Get our header/footer (this would probably be in a constructor actually
$this->data->layout = $this->layout_m->getLayout();
// Get user information using a session var (prob. another good line for the constructor
$this->data->user = $this->users_m->getUser($this->session->userdata('id'));
// Send it all to our view file
$this->load->view('welcome', $this->data);
Posted by Chris Hartjes on 02.11.07 at 4:46 pm
@Michael Wales
Thanks for the thoughts about CI! I guess I'm just big on consistency and my programmers brain twitches when it thinks about mixing OOP and procedural programming when there is no (as yet revealed to me) obvious reason for doing so in userland code (meaning not the functions that make the core of PHP itself).
Personally, I do all my dev work in PHP 5 and despite people's thinking on this matter CakePHP works just fine in PHP 5, there is just some code in the associative data mapping functionality that does checks based on what version of PHP you are running.
I assure you that it's valid code, because it does work. Now, perhaps I will go back and refactor it to be more along the lines of CI best practices, but in the effort to Get It Done I might have to leave it for now.
Posted by Dennison Uy - Graphic Designer on 02.11.07 at 4:46 pm
I have always been an OO guy so I love CakePHP but I am trying out CodeIgniter just so I can have a say for myself. The demos look real flashy though!
Posted by deltawing on 02.11.07 at 4:46 pm
After nightmarish experiences with spaghetti code and procedural/pseudo-OO programming, I can't stand even the tiniest inconsistencies anymore. That's why I chose CakePHP. As you said, "CakePHP has a much, much stronger commitment to using objects to access core functionality".
Posted by speedovation on 02.11.07 at 4:46 pm
CodeIgniter is really nice framework.My site is designed by using this.
Nice info...
Posted by Julien on 02.11.07 at 4:46 pm
I like the way you explain things.
Thanks for this post!