CAKEPHP2 Naming Conventions ____php

Source: Internet
Author: User
Tags lowercase naming convention

Category: Cake Tags: cake chinese document 4,312 people browse

Cake's naming conventions are quite important, and follow the naming conventions of the cake framework to get the various features built into the framework. After the release of CakePHP2, the naming convention does not differ from the old frame, and this article applies to the Cake 2.x and the 1.x frame.

Generally speaking, the filename is underlined and the class name is named by the hump. For example, the name of the Myniftyclass class corresponds to the my_nifty_class.php. Here are some examples of different types of classes and their corresponding file naming methods,

Controller class Kissesandhugscontroller, which can be found in the kisses_and_hugs_controller.php file (note the _controller in the filename).

Component Class Myhandycomponent, which can be found in the my_handy.php file.

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

The behavior class Especiallyfunkablebehavior can be found in the especially_funkable.php file.

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

Helper class Besteverhelper, which can be found in the best_ever.php file.

Each of these files is stored in the appropriate folder.

Summary: In the class name, in addition to the model class, you need to indicate the identity of the class in the class hit, such as Controller,component,behavior. In the file name, only the controller class needs to identify the identity, that is, add _controller at the end of the file. model and database naming conventions

The model class name uses the singular hump nomenclature, Person,bigperson and Reallybigperson are all contracted model names.

Table names are named with complex numbers and underscores. The table names corresponding to the above models are people,big_people and really_big_people respectively.

You can use the Inflector library to check the single plural form of a word.

A field name that contains two or more words, named after an underscore, such as first_name.

In a hasmany,belongsto,hasone relationship, the foreign key defaults to the related table plus _id. For example, there is a one-to-many relationship between Baker and Cake, and a baker_id foreign key is associated with the Bakers table in the Cakes table (Note that the associated foreign key here is singular). For multiple words of the table name, like Category_types, foreign key will be category_type_id. That is, although the table name is plural, the foreign key that exists in other tables is named in the singular.

A linked table typically exists in a many-to-many relationship, one named in the first alphabetical order of words, such as apples and zebras two tables, which in the linked table should be Apples_zebras, not zebras_apples.

All tables interacting with the cake model require a unique primary key to mark each row of data. Cake does not support combination of primary keys, if you want to directly manipulate linked table data, you can use the SQL statement directly.

In addition to using the Increment field as the primary key, you can also use char (36) as the primary key. When you save a record by using the Odel::save method, cake automatically recognizes the field and uses (STRING::UUID) to generate a 36-bit string as a unique primary key. controller Naming Conventions

The controller class name uses the plural hump nomenclature and ends with controller. Peoplecontroller and Latestarticlescontrolle are all legitimate controller naming methods.

The first method in the controller should be the index () method, which, when the request specifies only the controller and no action method, defaults to the index () method of the controller.

You can change the visible range of the controller method by underlining it before the controller method name. If the Controller method has an underscore in front of it, this method can be used internally only and not accessible through the browser. controller class name and URL address

The controller of a single word can be easily mapped to a lowercase URL address. For example, Applescontroller can be accessed through http://example.com/apples.

The access address of a multi-word controller can be any zigzag form 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 be a lowercase word underlined. Therefore,/red_apples/go_pick is the correct form of the request Redapplescontroller::go_pick method. View Naming Conventions

The view template file is named by calling their controller method, which separates multiple words by an underscore. The Getready () method in the Peoplecontroller class automatically associates the/APP/VIEWS/PEOPLE/GET_READY.CTP view template.

The naming method is/app/views/<controller NAME>/<UNDERSCORED_FUNCTION_NAME>.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

Using this naming convention, cake can map http://example.com/people/to the index () method of the Peoplecontroller controller, which can be used directly in the controller, The model is automatically bound to the people table and then outputs the view. All of this need not be configured, as long as you adhere to the cake's naming convention.

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.