CodeIgniter 的正确命名结构 [英] Correct naming structure for CodeIgniter

查看:20
本文介绍了CodeIgniter 的正确命名结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始我的第一个 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.

如果我希望公司页面的 url 为 http://example.com/Company/view

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

控制器需要调用 Company.php 正确吗?在公司控制器内部,它看起来像这样:

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');
}

好吧,我在这里很困惑,在上面的第 4 行:

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?

如果正确,模型文件是否需要命名为 Companymodel.php 并放置在 application/models 文件夹中?

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

示例:Company.php 并将其放在/application/controller/中然后将模型命名为 Company.php 并将其放置在应用程序/模型中,或者该模型应命名为 Companymodel.php

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

您的网址通常应全部为小写字母.如果您期望大写字母,您可能会意外排除小写字母,即使它们是相同的 URL.示例:www.example.com/controller/method/param

控制器类名应该全部小写,除了第一个字母.

Controller class names should be all lowercase, except the first letter.

  • 如果您的 URL 为 www.example.com/gallery,则控制器名称为 Gallery.
  • 如果您的 URL 为 www.example.com/admin_folder,则控制器名称为 Admin_folder.
  • 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.php
  • Admin_folder :: admin_folder.php

控制器方法也应该全部小写.大写字母有一定的灵活性,但与 URL 类似,它也有可能搞砸一些事情(这是一个示例,其中大写字母干扰了表单验证回调方法.

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).

模型遵循与控制器相同的大部分约定.唯一的区别是模型方法名称,它可以使用您对大写的偏好.由于这些方法与 URL 无关,并且使用普通的 PHP OOP 调用,因此您可以随意命名.

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.

建议使用全小写版本加载模型.虽然 CI 不需要它,但如果他们使用大写字母加载它可能会混淆一些用户,然后尝试以全部小写形式访问它(这是由于本机 PHP 对类属性 [和一般变量] 区分大小写),而不是 CodeIgniter).

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).

  • 模型类名:Users_model(_model 后缀也不是必需的,但有些人可能会使用它作为个人偏好,或者为了防止与 _model 后缀的命名冲突代码>用户控制器).
  • 模型文件名:users_model.php
  • 模型加载:$this->load->model('users_model')
  • 模型方法名称(一切正常):$this->users->getAll(), $this->users->find_by_name($name)
  • 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 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.

这些规则对于 CI 的库(位于 application/coreapplication/libraries 以及自定义或第三方库中)是相同的.

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

特别说明:在扩展默认 CI 库时,application/config.php 中定义的前缀会起作用.此前缀通常应全部为大写,后跟下划线.默认为 MY_.

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_.

  • 库类名称:Photos
  • 库文件名:Photos.php,
  • 库加载:$this->load->library('photos')

Helper 名称和加载都是小写的.文件名由助手名称和 _helper 组成.

Helper names and loading are all lowercase. The filename consists of the helper name with _helper appended after.

  • 助手名称:url
  • 帮助文件名:url_helper.php
  • 辅助加载:$this->load->helper('url')

CodeIgniter 的命名约定有些不一致,但确实没有太多的规则,所以它们很容易习惯和记忆.我很少在 CI 中遇到命名和加载问题,当我遇到问题时,通常是因为我只是在做一个与 Composer 相关的项目,所以我养成了不同的习惯.

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.

在撰写本文时,此答案中的规则适用于 CodeIgniter 2.1.x.在 Github 上有关于 3.0 的讨论,以更好地增加命名约定的一致性,你如果您愿意,可以阅读相关内容并做出贡献.

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.

这篇关于CodeIgniter 的正确命名结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆