Yii2 in-depth study-entry file

Source: Internet
Author: User
Tags autoload php framework

Some time ago, try to write a simple PHP framework, found that they are still missing a lot, and temporarily stopped. Ready to write read Yii2 source, and then go to read the source of Laravel, and finally continue to write this simple PHP framework.

Then the study of YII2 is taking the basic project for the time being as an example.

Let's take a look at the Yii2 's entry file first. When configuring the Nginx configuration of the Yii2, there are a few sentences

    # server_name mysite.local;    Root        /path/to/basic/web;    ......     / {        # Redirect everything that isn't a real file to index.php        try_files $uri $uri// Index.php? $args;    }

You can see that the portal file for the Web is the index.php file under the Web folder.

The contents of the index.php file are as follows:

<?PHP//comment out the following and lines when deployed to production//defines the tag of the debugdefined(' Yii_debug ') orDefine(' Yii_debug ',true);//define the environment, there are ' dev ' and ' prod ' twodefined(' yii_env ') orDefine(' yii_env ', ' Dev ');//introducing the autoload.php file in vendor, the class is automatically loaded based on the composer mechanismrequire(__dir__. ‘/.. /vendor/autoload.php ');//file yii.php with YII framework introducedrequire(__dir__. ‘/.. /vendor/yiisoft/yii2/yii.php ');//introduce the Web Config file and put the return value as the configuration item into the $config variable$config=require(__dir__. ‘/.. /config/web.php ');//new An instance of Yii\web\application and executes its Run method//with $config as a parameter for yii\web\application initialization(NewYii\web\application ($config))->run ();

Can see its core code, only the last sentence, all our requests are processed by the Run method to invoke execution, the internal content will be explained after.

Yii2 actually has another entry, which is the YII2 command line entry file, the Yii file under the top-level directory.

The contents of the Yii file are as follows:

#!/usr/bin/env PHP<?PHPdefined(' Yii_debug ') orDefine(' Yii_debug ',true);//fcgi doesn ' t has STDIN and STDOUT defined by default//define STDIN and STDOUTdefined(' STDIN ') orDefine(' STDIN ',fopen(' Php://stdin ', ' R '));defined(' STDOUT ') orDefine(' STDOUT ',fopen(' Php://stdout ', ' W '));require(__dir__. '/vendor/autoload.php ');require(__dir__. '/vendor/yiisoft/yii2/yii.php ');//introduce the config file of the console and put the return value as the configuration item into the $config variable$config=require(__dir__. '/config/console.php ');//new An instance of Yii\console\application and executes its Run method//with $config as a parameter for yii\console\application initialization$application=NewYii\console\application ($config);$exitCode=$application-run ();//ExitExit($exitCode);

The biggest difference from the index.php file is that it uses the Yii\console\application class, and the yii\web\application used in index.php.

This is the YII2 two entrance, if it is advanced project, the entrance will be more, but the basic content is one of these two forms.

Today is just a simple opening, first come here.

YII2 source interested students can pay attention to the project yii2-2.0.3-annotated, now on the above has been added a lot of comments on Yii2 source code, and then continue to add ~

Interested students can also participate in the submission of YII2 source comments.

Yii2 in-depth study-entry file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.