Yii Source Reading notes (i)

Source: Internet
Author: User

Today began to read the source of Yii2, want to learn more about the work of the YII framework, while learning a good coding standards and style. Take a look at the little tips in reading here.

Each frame has a portal file, starting with the portal file, Yii2 's portal file is located in the Web directory's index.php, which starts the Web app and configures some path parameters.

index.php--

1 //Comment out the following and lines when deployed to production2 defined(' Yii_debug ') orDefine(' Yii_debug ',true);3 defined(' yii_env ') orDefine(' yii_env ', ' Dev ');4 5 require(__dir__. ‘/.. /vendor/autoload.php ');6 require(__dir__. ‘/.. /vendor/yiisoft/yii2/yii.php ');7 8 $config=require(__dir__. ‘/.. /config/web.php ');9 Ten(NewYii\web\application ($config))->run ();

Lines 2nd and 3rd are in the form of short-circuiting statements ("or" can be replaced with "| |" Defines the current run mode and environment, and if Yii_debug is already defined, the statements after or are not executed. If yii_debug is defined, then the current debug state is present, and the application will have some output of debugging information during the run. When an exception is thrown, there will also be a detailed display of the call stack. By default, Yii_debug should be ture . However, in the development process, it is best to define true as described above so that errors are easily found and parsed.

If yii_env is defined, it specifies the running environment for the current application. The code above shows that the app will run in the Dev environment. By default, yii_env represents the product environment for prod .

These environments are just a name, the specific meaning and environment content depends on the definition of the environment. Dev prod is the default two environments after Yii installation, which represents the development environment and the final product environment, respectively. There is also a test environment that represents the testing environment.

The environment is different from the model. The environment is primarily affected by the configuration file in the code. yii_env 's dev prod test Three environments, respectively, make yii_env_dev yii_env_prod The value of yii_env_test is true . In this way, in the configuration of the application, especially in the same configuration file, different environments can be configured differently.

The 5th line of require introduces the autoload.php below vendor , __dir__ is the path to the current script, equivalent to DirName (__file__).

The 6th Line introduces the yiisoft/yii2/yii.php below vendor , which is a tool class file for Yii. With the introduction of this class file, it is possible to use the various tools provided by Yii to Yii::createobject () yii:: $app things can be used.

The 8th line is used to introduce the web.php configuration file under the configuration file;

Finally, the application class is instantiated with $config, and the Run () method is called to launch the application.

Next, look at the yiisoft/yii2/yii.php below vendor .

vendor/yiisoft/yii2/yii.php--

1 require(__dir__. '/baseyii.php ');2 3 4 classYiiextends\yii\baseyii5 {6 }7 8Spl_autoload_register ([' Yii ', ' autoload '),true,true);9YII::$classMap=require(__dir__. '/classes.php ');TenYII::$container=NewYii\di\container ();

According to the annotations, this is a framework of auxiliary classes, you can write your own method to customize the functionality of Yiibase.

Yii Source Reading notes (i)

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.