Yii Framework official guide series 13-basic knowledge: Development specifications

Source: Internet
Author: User
Yii prefers specification over configuration. Compliance with specifications allows you to create mature Yii applications without the need to write and maintain complex configurations. Of course, if necessary, Yii can still be customized in almost all aspects through configuration. Below...


Yii prefers specification over configuration. Compliance with specifications allows you to create mature Yii applications without the need to write and maintain complex configurations. Of course, if necessary, Yii can still be customized in almost all aspects through configuration.

The following describes the development specifications recommended in Yii programming. For simplicity, we assume that WebRoot is the directory where the Yii application is installed.

1. URL

By default, Yii identifies URLs in the following format:

#

rThe GET variable indicates a route. it can be resolved as a controller and action by Yii. IfActionIDIf it is omitted, the controller uses the default action (defined in CController: defaultAction). IfControllerIDIs also omitted (orrThe application uses the default controller (defined in CWebApplication: defaultController ).

With the help of CUrlManager, you can create more identifiable and SEO-friendly URLs, such#. This feature is described in detail in URL Management.

2. code

Yii recommends that you use the camper style when naming variables, functions, and classes. that is, the first letter of each word is capitalized and connected together with no space in the middle. Variable names and function names should make all their first words lowercase to distinguish them from class names (for example:$basePath,runController(),LinkPager). For private class member variables, we recommend the following line as its name prefix (for example:$_actionList).

Because namespace is not supported before PHP 5.3.0, we recommend that you use an independent method to name classes to avoid conflicts with third-party classes. In view of this, all Yii Framework class names are prefixed with "C.

A special rule for controller names is that they must use wordsControllerEnd. The controller ID is the first letter of the class name in lowercase and the word is removed.Controller. For example,PageControllerThe class ID ispage. This rule makes the application more secure. It also makes the controller-related URL simpler (for example/index.php?r=page/indexInstead/index.php?r=PageController/index).

3. configuration

The configuration is an array of key-value pairs. Each key represents the property name in the configured object, and each value is the initial value of the corresponding property. For example,array('name'=>'My application', 'basePath'=>'./protected')InitializednameAndbasePathAttribute is their corresponding array value.

Any writable attribute in the class can be configured. If not configured, the default values of the attributes are used. When configuring an attribute, you are advised to read the relevant documentation to ensure that the initial value is correct.

4. file

Naming and usage of files depends on their types.

Class files should be named after the public classes they contain. For example, the CController class is located inCController.phpFile. A public class is a class that can be used by any other class. Each class file should contain up to one public class. Private classes (classes that can only be used by one public class) can be placed in the files where the public classes that use this class are located.

The view file should be named after the view. For example,indexThe view is located inindex.phpFile. A view file is a PHP script file that contains HTML and PHP code used to render content.

The configuration file can be named as needed. The configuration file is a PHP script. its main purpose is to return an Associated array that reflects the configuration.

5. Directory

Yii assumes that a series of default directories are used in different scenarios. If needed, each directory can be customized.

  • WebRoot/protected: This is the basic application directory, where all security-sensitive PHP scripts and data files are stored. Yii has a defaultapplicationThe alias points to this directory. Files in this directory and directory should be protected to prevent Web users from accessing them. It can be customized through CWebApplication: basePath.

  • WebRoot/protected/runtime: This directory stores private temporary files generated by the application at runtime. This directory must be writable to Web server processes. It can be customized through CApplication: runtimePath.

  • WebRoot/protected/extensions: This directory contains all third-party extensions. It can be customized through CApplication: extensionPath.

  • WebRoot/protected/modules: This directory contains all application modules. each module uses a sub-directory.

  • WebRoot/protected/controllers: This directory contains all controller files. It can be customized through CWebApplication: controllerPath.

  • WebRoot/protected/views: This directory contains all attempted files, including the controller view, Layout view, and system view. It can be customized through CWebApplication: viewPath.

  • WebRoot/protected/views/ControllerID: This directory stores view files used in a single controller class. HereControllerIDThe ID of the controller. It can be customized through CController: viewPath.

  • WebRoot/protected/views/layouts: This directory contains all Layout view files. It can be customized through CWebApplication: layoutPath.

  • WebRoot/protected/views/system: This directory contains all system view files. The system view file is a template used to display exceptions and errors. It can be customized through CWebApplication: systemViewPath.

  • WebRoot/assets: This directory contains public resource files. Resource files are private files that can be published and accessed by Web users. This directory must be writable to Web server processes. It can be customized through CAssetManager: basePath

  • WebRoot/themes: Different topics used by applications are stored in this directory. Each subdirectory is a topic, and the topic name is the directory name. It can be customized through cthemanager: basePath.

6. Database

Most Web applications are database-driven. For the best time, we recommend that you use the following naming rules when naming tables and columns. Note that these specifications are not required by Yii.

  • Both the database table name and column name are in lowercase.

  • Words in the name should be separated by underscores (for exampleproduct_order).

  • For table names, you can use either singular or plural. But do not use both. For simplicity, we recommend that you use singular names.

  • The table name can use a common prefix, for exampletbl_. This is especially useful when the tables used by the application coexist with the tables used by another application in the same database. The two tables can be easily separated by using different table prefixes.

The above is the Yii Framework official guide series 13-Basic Knowledge: development standard content. For more information, see PHP Chinese network (www.php1.cn )!

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.