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 go through the entry script that instantiates the application and forwards 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, usually named index.php, or other names that can be located by the Web server.
The entry script of the console application is generally named yii (suffix .php) in the application root directory. The file needs to have execution permissions so that the user can pass the command ./yii
The entry script mainly completes the following work:
Define global constants;
Register Composer autoloader;
Contain [[Yii]] class files;
Load application configuration;
Create an application instance and configure it;
Call [[yiibaseApplication::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 of 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); // fcgi 默认没有定义 STDIN 和 STDOUT defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w')); // 注册 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
The entry script is the best way to define global constants Here, 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 piece of code is more concise 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.