tags:

views:

1751

answers:

6

What are the best practices for CodeIgniter?

+1  A: 

One example would be to not include the ./system/application/config.php and ./system/application/database.php in the source code control. Better yet, rename config.php to something like config.default.php containing the default configurations that will more likely be similar across environments (dev/production) so it would be easier for others accessing the files to modify and rename to their respective names.

Randell
What is the rationale for keeping system-critical files out of source control?
Summer
Those are configuration files which do not have the same values across different development environments. You wouldn't want to checkout somebody else's configs and modify your configs everytime another user modifies his configs.
Randell
+1  A: 

Prefix or suffix your controllers and models with Controller_ and Model_ respectively (like in Kohana). Often there will be times when a controller and a model might have same names.

presario
I wonder if this problem can be solved with the addition of namespaces in PHP 5.3.
Randell
CodeIgniter is made to support PHP 4 too. So, if you use PHP 5 features then you should consider switching to Kohana.
presario
+2  A: 

There are many community developed best practices in CodeIgniter simple because it is so loose. For example, you can have your own naming conventions for controllers and models. I use the singular word for the front end controller, the plural word for my backend controller and then the singular word + model for my model names. Here's an example.

Front end: page.php Back end: pages.php Model: pagemodel.php

You can also move the 'application' directory out of the 'system' directory. This helps keep a clear separation between the framework core and your own application. It also makes sharing one CI install between multiple projects easy and also makes updating CodeIgniter very simple as well.

These are just a few but there are sites out there that will help you learn CodeIgniter. My own website programmersvoice.com has been described as a good resource.

+4  A: 

CodeIgniter has an official style guide:

http://codeigniter.com/user_guide/general/styleguide.html

jimyi
+2  A: 

Do not end your controller and model classes with ?> to close the php tag. Just leave it open.

Make sure there are no whitespaces before the opening <?php tag, would cause some HTTP header issues.

Great advice! I have spent a couple of hours trying to figure out hy my RSS feed isn't valid because of the blank space in one of the controllers!
Mike
Very wise additional tip from CI user guide: "... instead, use a comment block to mark the end of file ... [so you can] still identify a file as being complete and not truncated."
Smandoli
+1  A: 

My tip would be to keep everything as abstract as possible.

Don't put all of your business logic in your controllers, because if you find one day you need to use the same logic in different controllers or methods, you'll be stumped.

Put as much business logic in your models/libraries as you can. Some people only generally keep database abstraction layer in their models, some people put everything in - it's up to you. The best thing to do is find an approach you like and stick with it. It won't come to you instantly, give it chance. Good programmers are made from experience and doing things wrong.

In general, keep your models FAT, controllers SKINNY, and views DUMB.

This means like I say, most logic in your models, only very fine abstraction between model <-> view in your controller, and ONLY presentation logic in your view.

If you have to do any complex logic in your views, you have structured your code incorrectly.

Hope that helps.

George