Yii Framework Official Guide Series 13 - Basics: Development Specifications

黄舟
Release: 2023-03-05 17:46:02
Original
1354 people have browsed it


Yii prefers specification over configuration. Following the specification allows you to create mature Yii applications without writing and maintaining complex configurations. Of course, Yii can still be customized in almost every aspect through configuration if necessary.

Below we explain the recommended development specifications in Yii programming. For simplicity, let's assume that WebRoot is the directory where the Yii application is installed.

1. URL

By default, Yii recognizes URLs in the following format:

http://www.php.cn/
Copy after login

r The GET variable means route, which Can be parsed by Yii into controllers and actions. If ActionID is omitted, the controller will use the default action (defined in CController::defaultAction); if ControllerID is also omitted (or the r variable is not exists), the application will use the default controller (defined in CWebApplication::defaultController).

With the help of CUrlManager, you can create a more identifiable and SEO-friendly URL, such as http://www.php.cn/. This feature is explained in detail in URL Management.

2. Code

Yii recommends using camel case style when naming variables, functions and classes, that is, the first letter of each word is capitalized and connected together, with no spaces in between. Variable and function names should have their first word lowercase to distinguish them from class names (for example: $basePath, runController(),LinkPager). For private class member variables, we recommend prefixing their names with an underscore (for example: $_actionList).

Since namespaces are not supported before PHP 5.3.0, we recommend that classes be named in some independent way to avoid conflicts with third-party classes. For this reason, all Yii framework class names are prefixed with "C".

A special rule for controller names is that they must end with the word Controller. Then the controller ID is the first letter of the class name in lowercase and removes the word Controller. For example, the ID of the PageController class is page. This rule makes the application more secure. It also makes controller-related URLs simpler (e.g. /index.php?r=page/index instead of /index.php?r=PageController/index).

3. Configuration

Configuration is an array of key-value pairs. Each key represents a 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') initializes name and basePath Properties are their corresponding array values.

Any writable property in the class can be configured. If not configured, properties will use their default values. When configuring a property, it's a good idea to read the documentation to ensure the initial values ​​are correct.

4. Files

The conventions for naming and using files depend on their type.

Class files should be named after the public classes they contain. For example, the CController class is located in the CController.php file. A public class is a class that can be used by any other class. Each class file should contain at most one public class. Private classes (classes that can only be used by one public class) can be placed in the same file as the public class that uses them.

View files should be named after the view. For example, the index view is located in the index.php file. A view file is a PHP script file that contains the HTML and PHP code used to render content.

The configuration file can be named arbitrarily. A configuration file is a PHP script whose main purpose is to return an associative array that embodies the configuration.

5. Directory

Yii assumes a series of default directories for different situations. Each directory can be customized if needed.

  • WebRoot/protected: This is the application base directory, where all security-sensitive PHP scripts and data files are placed. Yii has a default application alias pointing to this directory. This directory and the files in it should be protected from access by web users. It can be customized via CWebApplication::basePath.

  • WebRoot/protected/runtime: This directory contains private temporary files generated when the application is running. This directory must be writable by the web server process. It can be customized via CApplication::runtimePath.

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

  • WebRoot/protected/modules: This directory places all application modules, and each module uses a subdirectory.

  • WebRoot/protected/controllers: This directory places all controller class files. It can be customized via CWebApplication::controllerPath.

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

  • WebRoot/protected/views/ControllerID: This directory places the view files used in a single controller class. ControllerID here refers to the ID of the controller. It can be customized via CController::viewPath.

  • WebRoot/protected/views/layouts: This directory places all layout view files. It can be customized via CWebApplication::layoutPath.

  • WebRoot/protected/views/system: This directory contains all system view files. System view files are templates used to display exceptions and errors. It can be customized via CWebApplication::systemViewPath.

  • WebRoot/assets: This directory places public resource files. Resource files are private files that can be published and accessed by web users. This directory must be writable by the web server process. It can be customized through CAssetManager::basePath

  • WebRoot/themes: This directory places different themes used by the application. Each subdirectory is a topic, and the name of the topic is the name of the directory. It can be customized via CThemeManager::basePath.

6. Database

Most web applications are driven by databases. For optimal time, we recommend using the following naming convention when naming tables and columns. Note that these specifications are not required for Yii.

  • Database table names and column names are named in lowercase.

  • Words in the name should be separated by underscores (e.g. product_order).

  • For table names, you can use either the singular or the plural. But don't use both at the same time. For simplicity, we recommend using singular names.

  • Table names can use a common prefix, such as tbl_. This is particularly useful when the table used by an application coexists in the same database as a table used by another application. The tables of these two applications can be easily distinguished by using different table prefixes.

The above is the content of Yii Framework Official Guide Series 13 - Basic Knowledge: Development Specifications. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template