CodeIgniter
- May 12th, 2007
- 8:43 PM (GMT-5)
- Digg This Post!
- Submit to reddit!
- Bookmark This
- 15 Comments posted
- 2 pingbacks received
- Discussion Feed
- 4,368 views
Since I have started at my new job we have been looking into several different PHP frameworks to use for the new development. We pondered doing a custom framework for the site but in the end we opted to go with CodeIgniter. I have to say that overall CodeIgniter is a really nice framework. It saves us a lot of time and code when we want to do simplistic things. The best part about CodeIgniter is that it is based on the MVC design pattern.
MVC is a really nice method to use for object oriented development. It keeps things well organized and keeps the code clean, organized and easy to manage. The framework is also very easy to extend. You can extend core classes or you can replace them with your own implementations. It also gives you the ability to create your own libraries and helpers. A library is a class which you can load into CodeIgniter. Below is an example of a controller that will load a custom library and put it to use.
-
class Info extends Controller
-
{
-
public function index()
-
{
-
// loading the users model to get the user data
-
$this->load->model("users");
-
-
$userId = 1;
-
$data = $this->users->getUserInfo( $userId );
-
-
// loading the E-Mail library to send an E-Mail
-
$this->load->library('email');
-
-
$this->email->from('your@your-site.com', 'Your Name');
-
$this->email->to($data['email']);
-
$this->email->subject('Email Test');
-
$this->email->message('Testing the email class.');
-
-
$this->email->send();
-
}
-
}
You would have to save this file in the controllers directory as info.php and to access this page you would go to http://www.domain.com/info/
All this page will do is load up the user model, call a getUserInfo method passing it in the id of 1. This is just an example so all of the code is not written for you. I assumed the method returned an array and passed $data['email'] to the email library that will send the email.
You can see how easy it is to use. We have highly customized the installation on our development environment so that we can have dynamic sub-domains. All sub-domains will use the same application directory. Our directory structure looks like the following
applications
- controllers
-- www
-- auth
-- jobs
- models
- views
-- www
-- auth
-- jobs
Now because all of our sub-domains use the same application directory we had to setup some mod_rewrite to handle this. If you go to http://www.ere.net/article/23245/ you will really be going to http://www.ere.net/www/article/23245/
Notice that you will see the www after the domain name but before the article parameter. This is set to let CodeIgniter know that it needs to look in the www sub-directory of the controllers and view folders.
So far the system is working great for us however we have run into a few issues that I hope the developers take care of in the near future.
- Controllers only allow 1 directory depth so you cannot have for instance controllers/dir1/dir2/dir3/controller.php. In our case this is causing an issue because we have erejobs.com that is going to mod_rewrite back to jobs.ere.net Now if you go to say my.erejobs.com it would rewrite to jobs.ere.net/jobs/my/controller.php. You can see how this will not work and we would have to use my.php as the main controller for the entire sub-domain. This will not work in our situation so I am going to have to look into altering CodeIgniter to allow for multiple sub-directories.
- The database classes that CodeIgniter uses seems to very loosely use the active record pattern. However from all the documentation that I have been reading about the active record pattern CodeIgniter does not seem to use it properly. I might also have to look into a way to override the default database stuff in CodeIgniter. This is the one library that they do not make it easy to override so it will possibly lead to some core customizations. In our situation I think it would be better for us to use the data mapper pattern rather than the active record pattern anyway.
- The form validation in CodeIgniter does not handle the check boxes or radio buttons very well. The very nature of HTML forms is that when you post a form and a check box is not checked a value will not be set in the $_POST super global for that form element. However with CodeIgniter you can set a required rule for a check box or radio button but when the form validation takes place it has some issues with being able to validate the form values. You would think that if you set a required rule it would fail if it check if(!isset($_POST['field'])) //trigger error
We still have a LOT to do with this new rewrite of the ERE websites and I look forward to pushing my way through this and learning more about CodeIgniter. If you have any suggestions for my concerns above please let me know.
15 Comments
Joseph Crawford
- 05/13/07
- 11:56 AM
I think overriding the router class will be our best bet. I know that I can set routes but that will not help with the organization of the code. When i mentiones sub-directories I was meaning the actual directory structure and not the URL.
I am going to look into overriding the router class tomorrow when I am back to work
developercast.com » Joseph Crawford’s Blog: CodeIgniter
- 05/14/07
- 10:25 AM
- Pingback
[...] Crawford in looking for a PHP framework to work with at his job, decided to give CodeIgniter and liked what he saw. Since I have started at my new job we have been looking into several different PHP frameworks to [...]
Jenny
- 05/14/07
- 11:10 PM
omg im totally in love with your design! how awesome is this!! i must chat you up and get pointers...or get you to redesign my site. LOL.
Stuart
- 05/14/07
- 11:48 PM
You say, "The best part about CodeIgniter is that it is based on the MVC design pattern." but there are many frameworks that are also based on MVC.
I'd be more interested in hearing why you chose CodeIgnitor over, for example, Cake or Symfony. Those two are arguably as popular or more popular than CodeIgnitor so hearing why you rejected them for your choice would be most interesting.
cifollower
- 05/15/07
- 12:22 AM
1. True about the validation of checkboxes/radio buttons (and restoring their values on validation failure). It is difficult as well to provide validation/restoring values for post variables passed in the form of an array - choices[]=1&choices[]=2&choices[]=3.
2. Also, you should be aware of the lethal bug (or a daunting functionality as some put it) with CodeIgniter. See the discussion http://codeigniter.com/forums/viewthread/50715/. Basically you have to be really carefull when checking for user parameters on request to delete and UPDATE controllers as well since if an 'id' parameter was not passed and you failed to check for it in your controller, then the query generated with its Database library (if you do decide to use it) will be broken ("DELETE FROM 'table_name' WHERE", missing the '=' sign) and result in mass delete/update. This is not fixed in the latest CI 1.5.3 release.
3. And, finally, it is relatively difficult to internationalize a CI application.
For all its disadvantages, I have to say that CI is definately a worthy framework with flat learning curve, moderate following, and best performance. If you need something better in terms of configurability and fullness of features, but not performance and learning curve Symfony is an unmatched choice.
All the best
Joseph Crawford
- 05/15/07
- 01:36 AM
1. Jenny,
Thanks I am glad you like the site
It has some issues with IE 6 I just have not had the time to dig into the css and figure out why.
2. Stuart,
We choose to go with CI because it is a very lean framework. I am not sure about Symfony but I know cake has a lot to it that makes it a bit bloated. One thing I personally did not like about cake was the so called "oven". I hate when frameworks auto-generate code for you. I saw this even more recently with propel a database ORM library. CI also had a very easy learning curve and is well documented.
3. cifollower,
I was unaware about this bug, I will have to do some digging and get that fixed in our version at the very least. About the internationalization I do not believe we will be dealing with that, however I should probably find that out
I appreciate all of the feedback you guys have given me on your opinions of CI and what to watch out for. I will take them all into consideration and will have to get a few issues with CI resolved on my end. We are still early in the development stages but a change of frameworks is going to be out of the question at this point.
Edward
- 05/15/07
- 11:09 AM
Hi Joseph. V interesting post. Did you look into Qcodo at all? I'd be interested to hear your thoughts on it as a framework.
I conducted a similar investigation into web app frameworks about a year ago and found Qcodo to be streets ahead of Cake, Symphony and others.
PS: Why are you comments top-posting? It goes against convention and forces me to scroll to the bottom and then work upwards to read the conversations.
Joseph Crawford
- 05/15/07
- 11:17 AM
Edward,
I have not looked into that framework and do not think I will have the time to look into it anytime soon. I appreciate the suggestion and will take a look as soon as I get some time.
As for why my comments are top-posting is because it was easiest at the time
I will have to look into reversing the order of the comments however I do not recall where I set that. I think it had something to do with the comment paging I have setup.
spy phone
- 05/22/07
- 08:43 AM
this is what i was looking for! thanks for sharing with us.
Dr-Hamza’s Space
- 07/13/07
- 05:05 PM
- Pingback
[...] vs. CakePHP PHP Frameworks: CakePHP vs. Code Igniter Code Igniter - Ignite your PHP Application Meeting CodeIgniter Code Igniter: Installation and First Run , by Mizanur Rahman igniting code with code igniter , by [...]
Daniel Errante
- 11/05/07
- 04:36 PM
CodeIgniter is such an easy PHP framework to grasp as soon as you understand MVC programming. Your projects are automatically more organized using CI and the more you use it the more you learn how flexible the framework is. It's very lightweight and more PHP developers need to use it!
Robert O'Rourke
- 04/18/08
- 02:07 PM
Hi Joseph,
Have you managed to work around the single depth of sub folders for controllers issue yet? I'm trying to find a solution for it aswell.
Cheers
Joseph Crawford
- 04/19/08
- 07:03 AM
Robert,
I have not worked with CI since I was at this position with ERE Media. I am sorry but I am afraid I cannot be of any help.
Joe Taylor
- 09/21/08
- 11:15 PM
I live by Code Igniter at this point. Its extremely easy to use. It has a little of everything you'll need for a website or basic application. Database work is now a breeze. I've made at least 20 projects with it at this point.






Alex
Regarding the first point, you could override the default router class or create custom routes in the configuration file.