Yii2的深入学习-通道口文件
Yii2的深入学习--入口文件
前一段时间,尝试去写一个 php 的简单框架,发现自己还欠缺很多,就暂时停掉了。准备先读完 Yii2 的源码,然后再去看完 laravel 的源码,最后再继续去写这个简单的 php 框架。
之后关于 Yii2 的学习暂时都是以 basic 的项目为例。
我们先来看一下 Yii2 的入口文件。在配置 Yii2 的 nginx 的配置时,有这样几句
<span style="color: #000000;"> # server_name mysite.local; root </span>/path/to/basic/<span style="color: #000000;">web; ...... location </span>/<span style="color: #000000;"> { # Redirect everything that isn</span><span style="color: #800000;">'</span><span style="color: #800000;">t a real file to index.php</span> try_files $uri $uri/ /index.php?<span style="color: #000000;">$args; }</span>
可以看到web的入口文件是 web 文件夹下的 index.php 文件。
index.php 文件的内容如下:
<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;"> comment out the following two lines when deployed to production// 定义 debug 的标记</span><span style="color: #008080;">defined</span>('YII_DEBUG') or <span style="color: #008080;">define</span>('YII_DEBUG', <span style="color: #0000ff;">true</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 定义环境,有 'dev' 和 'prod' 两种</span><span style="color: #008080;">defined</span>('YII_ENV') or <span style="color: #008080;">define</span>('YII_ENV', 'dev'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 引入 vendor 中的 autoload.php 文件,会基于 composer 的机制自动加载类</span><span style="color: #0000ff;">require</span>(__DIR__ . '/../vendor/autoload.php'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 引入 Yii 框架的文件 Yii.php</span><span style="color: #0000ff;">require</span>(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 引入 web 的 config 文件,并将返回值即配置项放入 $config 变量中</span><span style="color: #800080;">$config</span> = <span style="color: #0000ff;">require</span>(__DIR__ . '/../config/web.php'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> new 一个 yii\web\Application 的实例,并执行它的 run 方法// 用 $config 作为 yii\web\Application 初始化的参数</span>(<span style="color: #0000ff;">new</span> yii\web\Application(<span style="color: #800080;">$config</span>))->run();
可以看到其核心代码,就只有最后一句,我们所有的请求的处理都是通过 run 方法去调用执行的,内部的具体内容之后会讲解。
Yii2 其实还有另外一个入口,是 Yii2 命令行的入口文件,即顶级目录下的 yii 文件。
yii 文件的内容如下:
<span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/env php</span><span style="color: #000000;">php</span><span style="color: #008080;">defined</span>('YII_DEBUG') or <span style="color: #008080;">define</span>('YII_DEBUG', <span style="color: #0000ff;">true</span><span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> fcgi doesn't have STDIN and STDOUT defined by default// 定义 STDIN 和 STDOUT</span><span style="color: #008080;">defined</span>('STDIN') or <span style="color: #008080;">define</span>('STDIN', <span style="color: #008080;">fopen</span>('php://stdin', 'r'<span style="color: #000000;">));</span><span style="color: #008080;">defined</span>('STDOUT') or <span style="color: #008080;">define</span>('STDOUT', <span style="color: #008080;">fopen</span>('php://stdout', 'w'<span style="color: #000000;">));</span><span style="color: #0000ff;">require</span>(__DIR__ . '/vendor/autoload.php'<span style="color: #000000;">);</span><span style="color: #0000ff;">require</span>(__DIR__ . '/vendor/yiisoft/yii2/Yii.php'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 引入 console 的 config 文件,并将返回值即配置项放入 $config 变量中</span><span style="color: #800080;">$config</span> = <span style="color: #0000ff;">require</span>(__DIR__ . '/config/console.php'<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> new 一个 yii\console\Application 的实例,并执行它的 run 方法// 用 $config 作为 yii\console\Application 初始化的参数</span><span style="color: #800080;">$application</span> = <span style="color: #0000ff;">new</span> yii\console\Application(<span style="color: #800080;">$config</span><span style="color: #000000;">);</span><span style="color: #800080;">$exitCode</span> = <span style="color: #800080;">$application</span>-><span style="color: #000000;">run();</span><span style="color: #008000;">//</span><span style="color: #008000;"> 退出</span><span style="color: #0000ff;">exit</span>(<span style="color: #800080;">$exitCode</span>);
与 index.php 文件最大的区别在于,它使用的是 yii\console\Application 类,而 index.php 中使用的 yii\web\Application。
这就是 Yii2 的两个入口,如果是 advanced 的项目的话,入口会更多,但基本内容都是这两种形式之一。
今天只是一个简单的开篇,就先到这里。
对 Yii2 源码有兴趣的同学可以关注项目 yii2-2.0.3-annotated,现在在上面已经添加了不少关于 Yii2 源码的注释,之后还会继续添加~
有兴趣的同学也可以参与进来,提交 Yii2 源码的注释。
- 1楼茫海
- quot;准备写读完 Yii2 的源码,然后再去看完 laravel 的源码,最后再继续去写这个简单的 php 框架quot;,无意中看到你这个,简直跟我目前的计划不谋而合。注册的这个博客园账号就是打算用来记录这个的。
- Re: 疯狂的原始人
- @茫海,那就一起加油吧,有问题也可以一起讨论~~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
