CAKEPHP2 naming specification

Source: Internet
Author: User
Tags naming convention
CAKEPHP2 Naming conventions

?

CakePHP's naming conventions are important, and following the naming conventions of cakephp 's framework, you can get the various functions built into the framework. After the release of CakePHP2, the naming convention is no different from the old framework, and this article applies to cakephp 2.x and cakephp 1.x frameworks.

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

The controller class Kissesandhugscontroller, which can be found in the kisses_and_hugs_controller.php file (note the _controller in the file name).

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, which can be found in the especially_funkable.php file.

The 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 hit the class to identify the identity of the class, such as Controller,component,behavior. In the file name, only the controller class needs to be identified, that is, add _controller to the end of the file.

Model and database naming conventions

The model class name uses the singular hump naming method , Person,bigperson and Reallybigperson are all contract 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 singular and plural forms of words.

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

In a hasmany,belongsto,hasone relationship, the foreign key is represented by the related table plus _id by default. For example, if a relationship between Baker and cake is one-to-many, there will be a baker_id foreign key in the cakes table to correlate the Bakers table (note that the associated foreign key here is the singular). For a table name consisting of multiple words, like category_types, the foreign key will be category_type_id. That is, the table name is a complex number, but the foreign key exists in the other table and is named in the singular form.

In many-to-many relationships, there is a link table that is named in alphabetical order, such as apples and zebras two tables, in the linked table, they should be apples_zebras, not zebras_apples.

All the tables that the CakePHP model interacts with require a unique primary key to mark each row of data. CakePHP does not support combining primary keys, and if you want to manipulate linked table data directly, you can use SQL statements 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 using the Odel::save method, CakePHP automatically recognizes the field and uses (STRING::UUID) to generate a 36-bit string as the unique primary key.

Controller naming specification

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

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

You can change the visible range of the controller method by adding an underscore before the Controller method name. If there is an underscore before the Controller method, this method is only used internally and cannot be accessed 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 the 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 specifies that the URL address should be a lowercase word plus an underscore. Therefore,/red_apples/go_pick is the correct form for requesting the Redapplescontroller::go_pick method.

View Naming conventions

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

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

Summarize

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/people/to the index () method of the Peoplecontroller controller, which can be used directly in the controller. And the model is automatically bound to the people table, and then the view is output. All of these don't need to be configured, as long as you follow cakephp's naming conventions.

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