Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to name controllers and models in codeigniter

I am trying to make some basic web apps using code igniter and I have realized that I have a lot of elements calling controller methods which grab data from its corresponding model and I am trying to figure out the best way to name these methods which pretty much do the same thing.

For an example, say I want to grab all the users on my site. I would have a method in my controller named get_users() and then that method would load and call a method in my model named fetch_users() which would grab the users from the database and return the result_array to the controller and then from there to the view.

This seems a little redundant (Calling get_users() which calls yet another function named fetch_users())

I just want to know if this is the only way to do this sort of action, or if there is another "cleaner" way of doing it.

Hope this makes sense.

Thanks!

like image 387
Wil Prim Avatar asked Jul 27 '13 23:07

Wil Prim


2 Answers

I like to separate more code, to be more clear.

For example: USERS

I crate one controller class with name Users and with:

function index() {} // (where show the list), 
function add() {} // (add one user)
function edit($user_id) {} // (edit one user)
function view($user_id) {} // (view one user)
function delete($user_id) {} // (delete one user)

I create one model with name Users_model and with:

function get_list() {} // Get list of users
function get_one($user_id) {} // Get one user
function add() {} // add one user on db
function update($id, $data) {} // Update one user on db
function delete($id) {} // Delete one user on db

In this form I do with other things like (Blog, posts, comments etc).

like image 161
Erman Belegu Avatar answered Oct 19 '22 22:10

Erman Belegu


Dear if you are going to build a large application try to put each of your controllers and models in different folders and name all of your controllers as home or index and different name for each folders

for example:

you have a controller for assets so create a folder inside your applications controller folder and name it assets and than inside this assets create a controller name it home.php

 class Home extends CI_Controller
 {
   function __construct()
   {
       parent::__construct();
   }
 }

for models you should use the name of operations which your models perform for example for CRUD model do create something like this: crud_model

  class Crud_model extends CI_Model
  {
      function __construct()
      {
           parent::__construct();
      }

  }

for functions you should give function names which is understandable and separate each part with underlines for example if you have a function that gets total of users you may do write like this:

 function get_total_users()
 {}

 or for users function

 function users()
 {

 }

 for update, delete and insert the same way.
like image 38
MJ X Avatar answered Oct 20 '22 00:10

MJ X