Mike Mukul

Mike Mukul

10 Quick CodeIgniter Tips

Simplify your framework-building experience with 10 CodeIgniter Tips

Introduction
Whether you are a newbie to CodeIgniter or a CodeIgniter Pro, there’s always more you can learn to make the process easier. I have compiled 10 quick CodeIgniter tips to make your experience with CodeIgniter smoother.

Here are the first 5 CodeIgniter tips:

1. Follow the CI default structure.
CodeIgniter comes with the default MVC pattern structure. Follow this basic structure. It is fairly common for most frameworks and for CI as well. When using the MVC structure use Controllers for logins, Models for database interaction and Views for HTML.

2. Use CI form validations.
Codeigniter provides built-in form validation features, which are very easy to use. I would recommend using CI form validations. It provides you the facility to set the rules, run validations and display messages.

To set the rules you can use the following syntax:
$this->form_validation->set_rules();

Example:
$this->form_validation->set_rules('email', 'Email', 'required');

You can also set cascading rules like this:
$this->form_validation->set_rules('email', 'Email', 'required|max_length[12]|is_unique[users.email]');

3. Sanitize your inputs.
Always sanitize your inputs before submitting the data to the database. This is very important for the application to prevent SQL (Structured Query Language) injections and to store only valid data into the database. Be sure that you always clean the inputs.

In CodeIgniter you can use the following method to clean your inputs:
$employees = $this->security->xss_clean($employees);

By setting a global (config) setting in CodeIgniter, you can run this filter automatically each time there is a post requested or cookie data fetched.

$config['global_xss_filtering'] = TRUE;

Note: Sanitize_filename() is also used to cross-check the file inputs from the user.

4. Protect your site from Cross-Site Request Forgery (CSRF).
To protect the site from CSRF attacks always enable the CodeIgniter settings for CSRF protection. To enable it, open your config file and look for the code written below:

$config['csrf_protection'] = TRUE;

5. Try to use CI-preferred styling and commenting.
CodeIgniter provides an excellent set of styles and commenting to format your code well. It works best if everybody uses the same recommendation for the framework. That way other developers can understand the code you are writing.

Here are the next 5 CodeIgniter tips:

6. Use caching techniques like Query Caching.
CI provides the database class that is used to cache your queries and reduce the database load. CodeIgniter loads this class automatically. You don’t have to do it manually if caching is enabled. You can enable the cache inside the database.php file, under config directory.

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

Note: You can try different caching techniques as well, like memcached and CI3 also integrated with RedIs.

7. Remove index.php from the URLs.
Always remove the index.php URLs to SEO-friendly URLs. Change your .htaccess code to make it work?
For example:

To change config file:
$config['index_page'] = "index.php"
to
$config['index_page'] = ""

To change in your .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

8. Don’t use PHP code, use an alternative in CodeIgniter.
I recommend that you not write your own PHP code. Find the CI alternative for everything you want to implement.

9. Create helpers for your most-often-used functions.
For the most commonly used functions always create the helpers. Helpers are just a set of functions for any specific functionality or category. To use the helpers, you have to load them. They do not load by default. This is how to load a helper:
$this->load->helper('helper_name');

10. The Config directory should have all the configuration information.
Keep all configuration files under the config directory. If they are outside the directory you may not be able to find them as easily. In the long run, putting the files into the directory will help when you’re working on big projects.

Note: Always load what is required for your application. Don’t load anything that is not needed. For this, you can use the constructor of your controller, if you only want to load part of the functionality.

There are many other ways to simplify your work when building with CodeIgniter. I hope these 10 CodeIgniter tips will make your experience with CodeIgniter better. Questions? Contact us at solutions@ithands.com.

CodeIgniter Hands is a division of IT Hands. IT Hands is a full-service web development and outsourcing company in India. We provide affordable web development services to web companies & ad-agencies. We have served more than 300 clients with our expertise.

Subscribe to our blog: