简体   繁体   English

在codeigniter中命名控制器和模型的最佳方法

[英]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. 我正在尝试使用代码点火器制作一些基本的Web应用程序,我已经意识到我有很多元素调用控制器方法从相应的模型中获取数据,我试图找出最好的方法来命名这些方法做同样的事。

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. 我的控制器中有一个名为get_users()的方法,然后该方法将加载并调用我的模型中名为fetch_users()的方法,该方法将从数据库中获取用户并将result_array返回给控制器,然后从那里返回到视图。

This seems a little redundant (Calling get_users() which calls yet another function named fetch_users()) 这似乎有点多余(调用get_users()调用另一个名为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! 谢谢!

I like to separate more code, to be more clear. 我想分开更多的代码,更清楚。

For example: USERS 例如: USERS

I crate one controller class with name Users and with: 我创建一个名为Users控制器类,并使用:

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: 我创建了一个名为Users_model模型,并使用:

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). 在这种形式,我做其他事情,如(博客,帖子,评论等)。

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 亲爱的,如果您要构建一个大型应用程序,请尝试将每个控制器和模型放在不同的文件夹中,并将所有控制器命名为home或index,并为每个文件夹命名不同的名称

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 你有一个资产控制器,所以在你的应用程序控制器文件夹中创建一个文件夹,并命名为资产,而在这个资产内创建一个控制器名称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 对于模型,您应该使用模型执行的操作名称,例如CRUD模型,请执行以下操作: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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CodeIgniter:实现外部对象(不是模型,视图或控制器的对象)的最佳方法? - CodeIgniter: Best way to implement External Objects (Objects that aren't models, views, or controllers)? Codeigniter:在模型和控制器,模型和模型,控制器和控制器之间使用变量的最简单方法 - Codeigniter: easiest way to use variables between models and controllers, models and models, controllers and controllers 在控制器和模型之间移动数据-Codeigniter最佳实践 - Moving Data between Controllers and Models - Codeigniter Best Practices CodeIgniter模型和控制器混淆 - CodeIgniter Models & Controllers Confusion 使用Controller在CodeIgniter中实现标准PHP类的最佳方法 - Best way to implement standard PHP classes in CodeIgniter with Controllers CodeIgniter /胖模型/瘦控制器 - CodeIgniter / Fat Models / Skinny Controllers CodeIgniter MVC最佳实践-控制器 - CodeIgniter MVC best practice - Controllers CodeIgniter中的Wordpress函数? (控制器,模型和视图) - Wordpress Functions in CodeIgniter? (Controllers, Models and Views) CodeIgniter文件上传-胖模型皮肤控制器 - CodeIgniter File Upload - Fat models skin controllers 在Codeigniter PATH中调用Controllers模型和视图 - Call Controllers models and views in codeigniter PATH
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM