CodeIgniter for beginner: 5 things you should know

It is almost one year I’ve been using CodeIgniter framework for my projects. Personally, I like this PHP framework very much because it is small, easy to setup, no need to use command prompt and etc. Besides, the documentation is great!

However, some of my friends keep bugging me with simple questions about the framework, how to do this, how to do that, bla..bla..bla.., which are the answers already written in the documentation.

So, I’m writing this post so that they know what they should.

1. How to setup database connection.

To setup the connection, open the database.php in config folder or you can refer to the documentation for details. If errors happen such as “Cannot connect to the database with the given setting”, this is the file that you need to edit.

2. Active Record Class

CodeIgniter framework provide an active record/query builder class, which allows you to interact with database simply. You should follow the active record style so in the end, your code looks cleaner and easy to be maintained, not to mentioned that it provide safer queries.

3. How to remove index.php in URL

Basically, you just need to put a single htaccess file at the same level with index.php file and remove the word “index.php” in the config.php file. Actually, the code inside htaccess file is depend on your server. So, not all code works on the servers. I usually used the code below. You can refer to the wiki for details.

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

4. URI routing

Feels like your URL is too long? Or you want to make it looks better? For example: domain.com/this-is-my-controller/this-is-the-function/param You can change the URL to domain.com/sample/sample2/param using route.php in config folder. Refer URI routing wiki for more.

5. Use autoload feature

You can load libraries, model, or helper automatically using autoload.php in the config folder.

So, I hope, this post will help the newbies or any of you who still in dark, looking for some guide on CodeIgniter framework. Any comments from you guys are welcome. 😀

P/s: This post was written and published in January 2011.