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

AI Hentai Generator
Generate AI Hentai for free.

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
