Correct naming structure for CodeIgniter

I'm starting my 1st CodeIgniter project and want to get some advice before i start. I'm a little confused with how the name of the controller and models work.

If i want the url to my company page to be http://example.com/Company/view

the controller needs to be called Company.php correct? inside the company controller it would look like this:

public function viewAll()
{
    $this->load->model('Companymodel');
    $this->load->view('templates/header');
    $data['result'] = $this->Companymodel->viewAll();
    $this->load->view('company/viewAll', $data);
    $this->load->view('templates/footer');
}

ok im confused here, on line 4 above:

$this->load->model('Companymodel');

this call to the company model page needs to have 1st letter capital with the rest lower case?

if that's correct, does the model file need to be called Companymodel.php and placed inside the application/models folder?

is it bad practice to call the controller and model the same

example: Company.php and place it inside /application/controller/ and then have the model called Company.php and place it inside the application/model or should the model be called Companymodel.php

I guess my ultimate question is the naming convention of the controller and model files, and whether they can be upper case or not.


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.


models/admin.php

<?php

class Admin extends CI_Model {
...etc

controllers/company.php will include the admin model with

function galleryView()
{
    $this->load->model('Admin');

    $numRows = $this->Admin->getPhotoNum();
... etc

To browse to galleryView the URL would be mysite.com/company/galleryView

There is very good documentation and examples on the CodeIgniter site