CakePHP2 naming rules

Source: Internet
Author: User
CakePHP2 naming convention? CakePHP naming rules are very important. following the naming rules of the CakePHP framework, you can obtain various built-in functions of the framework. After CakePHP2 is released, the naming convention is no different from the old framework. This article also applies to the CakePHP2.x and CakePHP1.x frameworks. In general, the file name should be underlined, and the class name should use the camper method. For example, the name of the file corresponding to the MyNiftyClass class is CakePHP2.

?

CakePHP naming rules are very important. following the naming rules of the CakePHP framework, you can obtain various built-in functions of the framework. After CakePHP2 is released, the naming convention is no different from the old framework. This article also applies to the CakePHP 2. x and CakePHP 1. x frameworks.

In general,The file name is underlined, and the class name is identified by the camper name.. For example, the file name of the MyNiftyClass class is my_nifty_class.php. The following are examples of different types of classes and their corresponding file naming methods,

The controller class KissesAndHugsController can be found in the kisses_and_hugs_controller.php File (NOTE _ controller in the file name ).

The component class MyHandyComponent can be found in the my_handy.php file.

The model type OptionValue can be found in the option_value.php file.

Behavior class EspeciallyFunkableBehavior, which can be found in the especially_funkable.php file.

View class SuperSimpleView, which can be found in the super_simple.php file.

The helper class BestEverHelper can be found in the best_ever.php file.

Each of the above files is stored in the corresponding folder.

Summary: In the class name, except for the model class, you must specify the class identity in the class hit, such as Controller, Component, and Behavior. In the file name, only the controller class needs to be identified, that is, add _ controller at the end of the file.

Model and database naming rules

Model class names use the singular camper method, Person, BigPerson, and ReallyBigPerson are all agreed model names.

The table name is named by the combination of the plural and underscore.. The table names corresponding to the preceding model are people, big_people, and really_big_people.

You can use the "inflector" library to check the singular and plural forms of words.

The name of a field that contains two or more words. The name is underlined, for example, first_name.

In the hasMany, belonsto, and hasOne relationships, the foreign key is represented by the related table plus _ id by default. For example, if the relationship between a Baker and a Cake is one-to-many, a baker_id foreign key will exist in the cakes table to associate the bakers Table (note that the foreign key is a singular one here ). For a table name composed of multiple words, such as category_types, the foreign key will be category_type_id. That is, although the table name is a complex number, the foreign key in other tables must be named in the form of a singular number.

There is usually a chain table in the many-to-many relationship. one of the chain table fields must be named in alphabetical order of the first letter of the word. for example, two tables, apples and zebras, are in the chain table, they should be apples_zebras, not zebras_apples.

All tables with CakePHP model interaction require a unique primary key to indicate each row of data. CakePHP does not support combination of primary keys. if you want to directly operate the chain table data, you can directly use SQL statements.

In addition to using the incremental field as the primary key, you can also use char (36) as the primary key. When you use the odel: save method to save a record, CakePHP automatically recognizes this field and uses (String: uuid) to generate a 36-bit String as the unique primary key.

Controller naming rules

The Controller class name uses the plural camper name and ends with the Controller.PeopleController and LatestArticlesControlle are both legal controller naming methods.

The first method in the controller should be the index () method. when the request only specifies the controller and there is no action method, CakePHP executes the index () method of the controller by default.

You can change the visible range of the controller method by adding an underscore before the controller method name. If there is an underline in front of the controller method, this method can only be used internally and cannot be accessed through a browser.

Controller class name and URL

The controller of a single word can be easily mapped to a lower-case URL address. For example, ApplesController can use http://example.com/apples.

The access address of the multi-word controller can be any twists and turns of the controller name.

  • /RedApples
  • /RedApples
  • /Red_apples
  • /Red_apples

Can point to the index method of the RedApples controller. However, the naming convention stipulates that the url address should consist of lowercase words and underscores. Therefore,/red_apples/go_pick is the correct form for requesting the RedApplesController: go_pick method.

View naming rules

The view template file is named by calling their controller method. the following line separates multiple words. The getReady () method in the PeopleController class is automatically associated with the/app/views/people/get_ready.ctp view template.

The naming method is/app/views/ / . Ctp.

Summary

Database table name: people
Model class: person, Path app/models/person. php
Controller class: PeopleController, Path app/contollers/people_controller.php
View class: app/views/people/index. ctp

With this naming convention, CakePHP can map http://example.com/lele/ to the index () method of the lelecontrollercontroller. The Person model can be used directly in this controller, and the model is automatically bound to the people table, and then output the view. No configuration is required, as long as you follow the naming conventions of CakePHP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.