


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/
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 defaultapplication
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::basePathWebRoot/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)!

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Setting the httponly flag is crucial for session cookies because it can effectively prevent XSS attacks and protect user session information. Specifically, 1) the httponly flag prevents JavaScript from accessing cookies, 2) the flag can be set through setcookies and make_response in PHP and Flask, 3) Although it cannot be prevented from all attacks, it should be part of the overall security policy.

PHPsessionssolvetheproblemofmaintainingstateacrossmultipleHTTPrequestsbystoringdataontheserverandassociatingitwithauniquesessionID.1)Theystoredataserver-side,typicallyinfilesordatabases,anduseasessionIDstoredinacookietoretrievedata.2)Sessionsenhances

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version
