Best way to name controllers and models in codeigniter

I'm trying to make some basic web applications using a code igniter, and I realized that I have many elements that call controller methods that capture data from the corresponding model, and I'm trying to find a better way to name these methods that pretty much do that same thing.

As an example, let's say that I want to capture all the users on my site. I would have a method in my controller named get_users (), and then this method would load and call a method in my model called fetch_users () that would capture users from the database and return result_array to the controller, and then look from there.

This seems a bit redundant (Calling get_users (), which calls another function called fetch_users ())

I just want to know if this is the only way to do this, or if there is another β€œcleaner” way to do it.

Hope this makes sense.

Thanks!

+7
php codeigniter
source share
2 answers

I like to separate more code to be more clear.

For example: USERS

I am breaking up one controller class called 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 called 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 other things, such as (blog, posts, comments, etc.).

+11
source share

Dear, if you are going to create a large application, try placing each of your controllers and models in different folders and name all your controllers as home or indexes, and a different name for each folder

eg:

you have a controller for assets, so create a folder inside your application controller folder and name its assets, not inside these assets, create the controller name it home.php

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

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

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

for functions, you must give names of functions that are understandable and share each part with underscores, for example, if you have a function that receives the total number of users that you can write as follows:

  function get_total_users() {} or for users function function users() { } for update, delete and insert the same way. 
+1
source share

All Articles