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

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

问题描述

我开始我的第一个codeignitor项目,想得到一些建议,我开始之前。



如果我想要到我的公司网页的网址是http://domain.com/Company/view



控制器需要调用Company.php正确吗?
在公司控制器中它将是这样的:

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

}

p>

  $ this-> load-> model('Companymodel'); 

此调用公司模型页面需要有第一个字母大写, p>

如果这是正确的,模型文件是否需要调用Companymodel.php并放置在应用程序/ models文件夹中?



是不好的做法,调用控制器和模型相同



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



我想我的最终问题是控制器的命名约定和模型文件,以及它们是否可以大写。



感谢您能够对此进行任何修改。

解决方案

网址



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



控制器



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




  • 如果您的网址< c> www.example.com/gallery ,控制器名称为图库

  • www.example.com/admin_folder ,控制器名称为 Admin_folder



控制器文件名应与类名匹配,但全部为小写。




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



控制器方法也应该全部为小写。有一些大写字母的灵活性,但类似于URL,有一些机会,它可以goof一些东西(

模型


$ b $模型遵循大多数与控制器相同的约定。唯一的区别是模型方法名称,它可以使用您的大写偏好。因为这些方法不绑定到URL,并且使用正常的PHP OOP调用,您可以随意命名它们。



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




  • 模型类名: Users_model $ c> _model 后缀也不是必需的,但有些人可能会使用它作为个人偏好,或防止与用户控制器的命名冲突)。

  • 模型文件名: users_model.php

  • 模型加载: $ this-> load-> model('users_model')

  • 模型方法名称(all okay): $ this-> users-> getAll() $ this-> users-> find_by_name($ name)





库遵循相同的约定 strong>为文件名。在这种情况下,文件名应该与类名匹配。



与模型类似,建议使用小写名称加载库。



这些规则对于CI库(位于 application / core application / libraries

特殊注意:扩展默认CI库时,<$ c中定义的前缀$ c> application / config.php 这个前缀通常都应该是大写的,后跟一个下划线,默认是 MY _




  • 图书馆课程名称:照片

  • 库文件名: Photos.php

  • 库加载: $ this-> load-> ; library('photos')



助手



辅助程序名称和加载均为小写。文件名由 _helper 后面的辅助程序名称组成。




  • 助手名称: url

  • 助手文件名: url_helper.php

  • 辅助程式载入: $ this-> load-> helper('url') li>


注意



CodeIgniter在命名约定上有些不一致,规则太多,所以他们很容易习惯和记忆。我很少在CI中命名和加载的问题,当我这样做,通常是因为我只是在一个Composer相关项目,所以我有一个不同的习惯。



此答案中的规则是CodeIgniter 2.1.x的写作。有关于Github for 3.0的讨论,以便更好地并向命名约定添加更多一致性,您可以阅读并作出贡献如果你愿意。


Im starting my 1st codeignitor project and want to get some advice before i start. Im a little confused with how the name of the controller and models work.

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

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

}

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?

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

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.

thanks for being able to shed any light to this.

解决方案

URLs

Your URLs should typically be all lowercase letters. If you expect capital letters, there's a chance you could accidentally exclude their lowercase counterparts, even though they're the same URL. Example: www.example.com/controller/method/param

Controllers

Controller class names should be all lowercase, except 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 controller name is Admin_folder.

Controller file names should match the class name, but be all lowercase.

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

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

Models

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.

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

  • 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

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.

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

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

  • Library class name: Photos
  • Library file name: Photos.php,
  • Library load: $this->load->library('photos')

Helpers

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

  • Helper name: url
  • Helper file name: url_helper.php
  • Helper load: $this->load->helper('url')

Notes

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.

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.

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

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