Proper naming structure for CodeIgnitor

I am starting my first codeignitor project and want advice before starting. I am a little confused about how the name of the controller and models work.

If I want my company url to be http://domain.com/Company/view

The controller needs to be called Company.php correctly? inside the company controller it will 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'); 

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

If this is correct, do I need to display the Companymodel.php model file and fit inside the application / models folder?

Are you wrong to call the controller and simulate the same

example: Company.php and put it inside / application / controller / and then have a model called Company.php and put it in the application / model or if the model will be called Companymodel.php

I think my last question is the naming convention for controller and model files, and whether they can be uppercase or not.

Thank you for the opportunity to shed light on this.

+8
codeigniter
source share
2 answers

Url

Your URLs should usually contain all lowercase letters. If you expect uppercase letters, you can accidentally exclude their subordinate copies, although they have the same URL. Example: www.example.com/controller/method/param

Controllers

Controller class names must be lowercase than 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 name of the controller is Admin_folder .

The controller file names must match the class name, but all lowercase letters.

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

Controller methods must also be lowercase. There is some flexibility with uppercase letters, but similar to URLs, there are possibilities in which this can happen ( here is an example where the uppercase letters interfered with the form validation callback method).

Models

Models comply with most of the same conventions as controllers. The only difference is the names of the model methods that may use your preference for capitalization. Since these methods are not bound to URLs and are called using regular PHP OOP, you can name them whatever you want.

It is recommended that you download models using the entire line version. Although CI is not required, it can confuse some users if they load it with a capital letter, but then try to access it like all lowercase (this is because native PHP is case sensitive with class properties [and variables in general], and not CodeIgniter).

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

Libraries

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

As with models, it is recommended that you load libraries using lowercase names.

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

Special note: when extending CI libraries by default, the prefix, as defined in application/config.php , comes into play. Usually this prefix should be uppercase and then underline. The default is MY_ .

  • Library Class Name: Photos
  • Library File Name: Photos.php ,
  • Library loading: $this->load->library('photos')

Assistants

The names and loading helpers are lowercase letters. The file name consists of the helper name with _helper added after.

  • Helper Name: url
  • Helper File Name: url_helper.php
  • Auxiliary load: $this->load->helper('url')

Notes

CodeIgniter is somewhat inconsistent in its naming conventions, but there really aren't many rules, so it's easy to get used to and remember. I rarely have problems with naming and loading in CI, and when I do this, it's usually because I just worked on a project related to the composer, so I got a different habit.

The rules in this answer are for CodeIgniter 2.1.x at the time of this writing. Github is discussed from 3.0 to better and adds more consistency to naming conventions that you can read about and contribute if you want.

+35
source share

models / admin.php

 <?php class Admin extends CI_Model { ...etc 

Controllers / company.php will include an admin model with

 function galleryView() { $this->load->model('Admin'); $numRows = $this->Admin->getPhotoNum(); ... etc 

To go to the gallery, the URL will be mysite.com/company/galleryView

There is very good documentation and examples on the CodeIgniter website.

+1
source share

All Articles