The entry script is the first step in the application startup process. An application (whether it is a web application or a console application) has only one entry script. End-user requests instantiate the application through the entry script and forward the request to the application.
The entry script of the Web application must be placed in a directory that can be accessed by end users. It is usually named index.php. You can also use the Web server to Other names targeted.
The entry script of the console application is generally named yii (suffix .php) in the application root directory. The file requires execution permission so that the user can pass the command ./yii
The entry script mainly completes the following work:
Define global constants;
Register Composer autoloader;
Contains Yii class files;
Load application configuration;
Create an application instance and configure it;
Call yii\ base\Application::run() to handle the request.
Web Application
The following is the code of the basic application template entry script:
<?php defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); // 注册 Composer 自动加载器 require __DIR__ . '/../vendor/autoload.php'; // 包含 Yii 类文件 require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php'; // 加载应用配置 $config = require __DIR__ . '/../config/web.php'; // 创建、配置、运行一个应用 (new yii\web\Application($config))->run();
Console Application
The following is the entry script for a console application:
#!/usr/bin/env php <?php /** * Yii console bootstrap file. * * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); // 注册 Composer 自动加载器 require __DIR__ . '/vendor/autoload.php'; // 包含 Yii 类文件 require __DIR__ . '/vendor/yiisoft/yii2/Yii.php'; // 加载应用配置 $config = require __DIR__ . '/config/console.php'; $application = new yii\console\Application($config); $exitCode = $application->run(); exit($exitCode);
Define constants
Entry script It is the best place to define global constants. Yii supports the following three constants:
YII_DEBUG: identifies whether the application is running in debug mode. When in debug mode, the application will retain more log information, and if an exception is thrown, a detailed error call stack will be displayed. Therefore, debug mode is mainly suitable for use during the development phase, and the default value of YII_DEBUG is false.
YII_ENV: Identifies the environment in which the application runs. Please refer to the configuration chapter for details. The default value of YII_ENV is 'prod', which means the application runs in an online production environment.
YII_ENABLE_ERROR_HANDLER: Identifies whether to enable error handling provided by Yii. The default is true.
When defining a constant, it is usually defined using code similar to the following:
defined('YII_DEBUG') or define('YII_DEBUG', true);
The above code is equivalent to:
if (!defined('YII_DEBUG')) { define('YII_DEBUG', true); }
Obviously the first paragraph The code is simpler and easier to understand.
Constant definitions should be at the beginning of the entry script, so that when other PHP files are included, the constants will take effect.
The above is the detailed content of What is the use of the yii framework entry script?. For more information, please follow other related articles on the PHP Chinese website!