Cakephp III (I) Practice priority principle (Convention over configuration)

Source: Internet
Author: User
Cakephp 3 (I) Practice priority principle (Convention over configuration)
As mentioned in CakePHP 3 cookbook, CakePHP is a faithful practitioner of the principle of customary precedence. CakePHP provides a complete set of contract programming systems. The customary precedence principle is an important design pattern, which aims at unifying the problem of naming inconsistencies in the project process and effectively speeding up the version iteration update speed.

The naming conventions of the MVC three modules in Cakephp 3 are described in detail.

1) Controller naming (Controllers Convention)
The controller class is a complex number, camel orthography (camelcased), which ends with controllers. For example, order controller, written as Orderscontroller.
In the controller, you can customize several exposed methods, such as a public Function view () in order, and the URL corresponds to/orders/view.
If a controller class (such as Redapplescontroller) with multiple word combinations is encountered, its corresponding URL has four equivalent forms
-/redapples
-/redapples
-/red_apples
-/red_apples
Note: CakePHP has a complete set of routing rewriting mechanisms. The above URL can be rewritten as dashedroute, such as/red-apples/go-pick, corresponding to Redapplescontroller::gopick ()

2) file and class name naming (Model and Database conventions)
For controllers (Controller), such as Redapplescontroller, the corresponding file is called redapplescontroller.php
For components (Component), such as Myhandycomponent, the corresponding file is called myhandycomponent.php
For data table processing classes (table), such as appletable.php, the corresponding file is called appletable.php

3) schema and database naming rules
The table class is plural, camel orthography (camelcased), such as People,bigpeople,reallybigpeople. corresponding to the name in the database: plural, and underlined. such as people, Big_people, Really_big_people.

In the datasheet, the name of the foreign key (Foreign key), such as the Baker has a lot of cakes, then in the cake table, there are foreign key baker_id. As seen from the example, the foreign key is named singular _id

4) View naming (views Convention)
You can create a function in the controller, such as Peoplecontroller::getready (), in a folder that appears as People/get_ready.ctp

Use an example to summarize cakephp's naming conventions:
database table: People-plural, lowercase
Controller: peoplecontroller-plural, camel
Views: Peoplecontroller:: Index ()

The priority principle of cakephp practice seems cumbersome. In practice, however, CakePHP provides the command tool as ror,laveral for fast development. In the next section, I'll talk specifically about how to use the command tool.
  • 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.