The application subject is the object that manages the overall structure and life cycle of the Yii framework application system. Each Yii application system can only contain one application principal. The application principal is created in the entry script and can be accessed globally through the expression \Yii::$app.
信息: 当我们说“一个应用”,它可能是一个应用主体对象,也可能是一个应用系统, 是根据上下文来决定[译:中文为避免歧义,Application 翻译为应用主体]。
Yii has two application principals: web application principal and console application principal. As the name suggests, the former Mainly handles web page requests, and the latter handles console requests.
(Recommended Learning: YII Frames )
## As shown below, when the entrance script creates an application subject, It loads a configuration file and passes it to the application body.require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php'; // 加载应用主体配置 $config = require __DIR__ . '/../config/web.php'; // 实例化应用主体、配置应用主体 (new yii\web\Application($config))->run();
Application subject properties
There are many important properties to be configured in the application subject configuration file. These properties specify the running environment of the application subject. For example, the application body needs to know how to load the controller, where to save temporary files, etc. Below we briefly describe these properties.Required attributes
In an application, at least two attributes must be configured: id and basePath.id
The id attribute is used to distinguish the unique ID of other applications. Mainly used by programs. To facilitate collaboration, it is best to use a number as the application principal ID, but it is not required to be a number.basePath
basePath specifies the root directory of the application. The root directory contains all protected source code of the application system. In the root directory, you can see subdirectories such as models, views, and controllers corresponding to the MVC design pattern. You can use a path or path alias to configure the basePath attribute. The directories corresponding to both formats must exist, otherwise the system will throw an exception. The system uses the realpath() function to normalize the configured path. The basePath attribute is often used to derive some other important paths (such as runtime paths). Therefore, the system predefines @app to represent this path. Derived paths can be composed through this alias (for example, @app/runtime represents the path of the runtime).The above is the detailed content of How to understand the application body of Yii framework?. For more information, please follow other related articles on the PHP Chinese website!