Table of Contents
Yii2的深入学习--入口文件,yii2深入学习入口
Home Backend Development PHP Tutorial Yii2的深入学习--入口文件,yii2深入学习入口_PHP教程

Yii2的深入学习--入口文件,yii2深入学习入口_PHP教程

Jul 12, 2016 am 09:07 AM
yii2 Entrance study try document go deep of

Yii2的深入学习--入口文件,yii2深入学习入口

前一段时间,尝试去写一个 php 的简单框架,发现自己还欠缺很多,就暂时停掉了。准备先读完 Yii2 的源码,然后再去看完 laravel 的源码,最后再继续去写这个简单的 php 框架。

之后关于 Yii2 的学习暂时都是以 basic 的项目为例。

我们先来看一下 Yii2 的入口文件。在配置 Yii2 的 nginx 的配置时,有这样几句

<span>    # server_name mysite.local;
    root        </span>/path/to/basic/<span>web;
    ......

    location </span>/<span> {
        # Redirect everything that isn</span><span>'</span><span>t a real file to index.php</span>
        try_files $uri $uri/ /index.php?<span>$args;
    }</span>
Copy after login

可以看到web的入口文件是 web 文件夹下的 index.php 文件。

index.php 文件的内容如下:

<?<span>php

</span><span>//</span><span> comment out the following two lines when deployed to production
// 定义 debug 的标记</span>
<span>defined</span>('YII_DEBUG') or <span>define</span>('YII_DEBUG', <span>true</span><span>);
</span><span>//</span><span> 定义环境,有 'dev' 和 'prod' 两种</span>
<span>defined</span>('YII_ENV') or <span>define</span>('YII_ENV', 'dev'<span>);

</span><span>//</span><span> 引入 vendor 中的 autoload.php 文件,会基于 composer 的机制自动加载类</span>
<span>require</span>(__DIR__ . '/../vendor/autoload.php'<span>);
</span><span>//</span><span> 引入 Yii 框架的文件 Yii.php</span>
<span>require</span>(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'<span>);

</span><span>//</span><span> 引入 web 的 config 文件,并将返回值即配置项放入 $config 变量中</span>
<span>$config</span> = <span>require</span>(__DIR__ . '/../config/web.php'<span>);

</span><span>//</span><span> new 一个 yii\web\Application 的实例,并执行它的 run 方法
// 用 $config 作为 yii\web\Application 初始化的参数</span>
(<span>new</span> yii\web\Application(<span>$config</span>))->run();
Copy after login

可以看到其核心代码,就只有最后一句,我们所有的请求的处理都是通过 run 方法去调用执行的,内部的具体内容之后会讲解。

Yii2 其实还有另外一个入口,是 Yii2 命令行的入口文件,即顶级目录下的 yii 文件。

yii 文件的内容如下:

<span>#</span><span>!/usr/bin/env php</span>
<?<span>php
</span><span>defined</span>('YII_DEBUG') or <span>define</span>('YII_DEBUG', <span>true</span><span>);

</span><span>//</span><span> fcgi doesn't have STDIN and STDOUT defined by default
// 定义 STDIN 和 STDOUT</span>
<span>defined</span>('STDIN') or <span>define</span>('STDIN', <span>fopen</span>('php://stdin', 'r'<span>));
</span><span>defined</span>('STDOUT') or <span>define</span>('STDOUT', <span>fopen</span>('php://stdout', 'w'<span>));

</span><span>require</span>(__DIR__ . '/vendor/autoload.php'<span>);
</span><span>require</span>(__DIR__ . '/vendor/yiisoft/yii2/Yii.php'<span>);

</span><span>//</span><span> 引入 console 的 config 文件,并将返回值即配置项放入 $config 变量中</span>
<span>$config</span> = <span>require</span>(__DIR__ . '/config/console.php'<span>);

</span><span>//</span><span> new 一个 yii\console\Application 的实例,并执行它的 run 方法
// 用 $config 作为 yii\console\Application 初始化的参数</span>
<span>$application</span> = <span>new</span> yii\console\Application(<span>$config</span><span>);
</span><span>$exitCode</span> = <span>$application</span>-><span>run();
</span><span>//</span><span> 退出</span>
<span>exit</span>(<span>$exitCode</span>);
Copy after login

与 index.php 文件最大的区别在于,它使用的是 yii\console\Application 类,而 index.php 中使用的 yii\web\Application。

这就是 Yii2 的两个入口,如果是 advanced 的项目的话,入口会更多,但基本内容都是这两种形式之一。

今天只是一个简单的开篇,就先到这里。

 

对 Yii2 源码有兴趣的同学可以关注项目 yii2-2.0.3-annotated,现在在上面已经添加了不少关于 Yii2 源码的注释,之后还会继续添加~

有兴趣的同学也可以参与进来,提交 Yii2 源码的注释。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1063026.htmlTechArticleYii2的深入学习--入口文件,yii2深入学习入口 前一段时间,尝试去写一个 php 的简单框架,发现自己还欠缺很多,就暂时停掉了。准备先读完...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box &quot;Error 0x80004005: Unspecified Error&quot; will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? Mar 14, 2024 pm 02:07 PM

Quark Netdisk and Baidu Netdisk are currently the most commonly used Netdisk software for storing files. If you want to save the files in Quark Netdisk to Baidu Netdisk, how do you do it? In this issue, the editor has compiled the tutorial steps for transferring files from Quark Network Disk computer to Baidu Network Disk. Let’s take a look at how to operate it. How to save Quark network disk files to Baidu network disk? To transfer files from Quark Network Disk to Baidu Network Disk, you first need to download the required files from Quark Network Disk, then select the target folder in the Baidu Network Disk client and open it. Then, drag and drop the files downloaded from Quark Cloud Disk into the folder opened by the Baidu Cloud Disk client, or use the upload function to add the files to Baidu Cloud Disk. Make sure to check whether the file was successfully transferred in Baidu Cloud Disk after the upload is completed. That's it

What is hiberfil.sys file? Can hiberfil.sys be deleted? What is hiberfil.sys file? Can hiberfil.sys be deleted? Mar 15, 2024 am 09:49 AM

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

Detailed explanation of log viewing command in Linux system! Detailed explanation of log viewing command in Linux system! Mar 06, 2024 pm 03:55 PM

In Linux systems, you can use the following command to view the contents of the log file: tail command: The tail command is used to display the content at the end of the log file. It is a common command to view the latest log information. tail [option] [file name] Commonly used options include: -n: Specify the number of lines to be displayed, the default is 10 lines. -f: Monitor the file content in real time and automatically display the new content when the file is updated. Example: tail-n20logfile.txt#Display the last 20 lines of the logfile.txt file tail-flogfile.txt#Monitor the updated content of the logfile.txt file in real time head command: The head command is used to display the beginning of the log file

Detailed explanation of the role of .ibd files in MySQL and related precautions Detailed explanation of the role of .ibd files in MySQL and related precautions Mar 15, 2024 am 08:00 AM

Detailed explanation of the role of .ibd files in MySQL and related precautions MySQL is a popular relational database management system, and the data in the database is stored in different files. Among them, the .ibd file is a data file in the InnoDB storage engine, used to store data and indexes in tables. This article will provide a detailed analysis of the role of the .ibd file in MySQL and provide relevant code examples to help readers better understand. 1. The role of .ibd files: storing data: .ibd files are InnoDB storage

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Full analysis of Go language file renaming operation Full analysis of Go language file renaming operation Apr 08, 2024 pm 03:30 PM

The os.Rename function is used in Go language to rename files. The syntax is: funcRename(oldpath,newpathstring)error. This function renames the file specified by oldpath to the file specified by newpath. Examples include simple renaming, moving files to different directories, and ignoring error handling. The Rename function performs an atomic operation and may only update directory entries when the two files are in the same directory. Renames may fail across volumes or while a file is in use.

Create and run Linux ".a" files Create and run Linux ".a" files Mar 20, 2024 pm 04:46 PM

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension &quot;.a&quot; have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run &quot;.a&quot; files. This article will introduce how to comprehensively install and configure the Linux &quot;.a&quot; file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux &quot;.a&quot; file. What is L

See all articles