Correct naming structure for CodeIgniter

URLs

Your URLs should typically be all lowercase letters. If you expect capital letters, there’s a chance you could accidentally exclude their lowercase counterparts, even though they’re the same URL. Example: www.example.com/controller/method/param

Controllers

Controller class names should be all lowercase, except the first letter.

  • If your URL is www.example.com/gallery, the controller name is Gallery.
  • If your URL is www.example.com/admin_folder, the controller name is Admin_folder.

Controller file names should match the class name, but be all lowercase.

  • Gallery :: gallery.php
  • Admin_folder :: admin_folder.php

Controller methods should be all lowercase as well. There is some flexibility with uppercase, but similar to URLs, there are opportunities where it can goof something up (here’s an example where capital letters interfered with a form validation callback method).

Models

Models follow most of the same conventions as controllers. The only difference is with model method names, which can use your preference of capitalization. Since these methods are not tied to URLs, and are called using normal PHP OOP, you can name them as you please.

It is recommended to load models using the all lowercase version. While it is not required by CI, it can confuse some users if they load it with a capital letter, but then attempt to access it as all lowercase (this is due to native PHP being case sensitive with class properties [and variables in general], not CodeIgniter).

  • Model class name: Users_model (the _model suffix is also not required, but some people may use it as a personal preference, or to prevent naming conflicts with a Users controller).
  • Model file name: users_model.php
  • Model loading: $this->load->model('users_model')
  • Model method names (all okay): $this->users->getAll(), $this->users->find_by_name($name), etc.

Libraries

Libraries follow the same conventions except for the file name. In their case, file names should match the class name.

Similar to models, it’s recommended to load libraries using the lowercase name.

These rules are the same for CI’s libraries (located in application/core and application/libraries, as well as custom or third-party libraries.

Special note: when extending default CI libraries, the prefix as defined in application/config.php comes into play. This prefix typically should be all uppercase, followed by an underscore. The default is MY_.

  • Library class name: Photos
  • Library file name: Photos.php,
  • Library load: $this->load->library('photos')

Helpers

Helper names and loading are all lowercase. The filename consists of the helper name with _helper appended after.

  • Helper name: url
  • Helper file name: url_helper.php
  • Helper load: $this->load->helper('url')

Notes

CodeIgniter is somewhat inconsistent in their naming conventions, but there really aren’t too many rules, so they are easy to get used to and memorize. I very rarely have issues with naming and loading in CI, and when I do, it’s usually because I was just working on a Composer-related project so I got into a different habit.

The rules in this answer are for CodeIgniter 2.1.x as of this writing. There is discussion on Github for 3.0 to better and add more consistency to naming conventions, which you can read about and contribute to if you’d like.

Leave a Comment