Home >php教程 >PHP开发 >Yii2.0 entrance script

Yii2.0 entrance script

伊谢尔伦
伊谢尔伦Original
2016-11-25 14:23:011294browse

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 d31b1a7c44939764b30ad061c016a24f [arguments] [options] to run the console application.

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(&#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 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(&#39;YII_DEBUG&#39;) or define(&#39;YII_DEBUG&#39;, true);
    // fcgi 默认没有定义 STDIN 和 STDOUT
    defined(&#39;STDIN&#39;) or define(&#39;STDIN&#39;, fopen(&#39;php://stdin&#39;, &#39;r&#39;));
    defined(&#39;STDOUT&#39;) or define(&#39;STDOUT&#39;, fopen(&#39;php://stdout&#39;, &#39;w&#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

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(&#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 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.


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