Home >PHP Framework >YII >What is the use of the yii framework entry script?

What is the use of the yii framework entry script?

(*-*)浩
(*-*)浩Original
2019-11-28 15:31:522092browse

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.

What is the use of the yii framework entry script?

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 (Recommended learning: yii framework)

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(&#39;YII_DEBUG&#39;) or define(&#39;YII_DEBUG&#39;, true);
defined(&#39;YII_ENV&#39;) or define(&#39;YII_ENV&#39;, &#39;dev&#39;);

// 注册 Composer 自动加载器
require __DIR__ . &#39;/../vendor/autoload.php&#39;;

// 包含 Yii 类文件
require __DIR__ . &#39;/../vendor/yiisoft/yii2/Yii.php&#39;;

// 加载应用配置
$config = require __DIR__ . &#39;/../config/web.php&#39;;

// 创建、配置、运行一个应用
(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(&#39;YII_DEBUG&#39;) or define(&#39;YII_DEBUG&#39;, true);
defined(&#39;YII_ENV&#39;) or define(&#39;YII_ENV&#39;, &#39;dev&#39;);
// 注册 Composer 自动加载器
require __DIR__ . &#39;/vendor/autoload.php&#39;;
// 包含 Yii 类文件
require __DIR__ . &#39;/vendor/yiisoft/yii2/Yii.php&#39;;
// 加载应用配置
$config = require __DIR__ . &#39;/config/console.php&#39;;
$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(&#39;YII_DEBUG&#39;) or define(&#39;YII_DEBUG&#39;, true);

The above code is equivalent to:

if (!defined(&#39;YII_DEBUG&#39;)) {
    define(&#39;YII_DEBUG&#39;, 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!

Statement:
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