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: 疯狂的原始人
- @茫海,那就一起加油吧,有问题也可以一起讨论~~

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

Pour travailler avec la date et l'heure dans cakephp4, nous allons utiliser la classe FrozenTime disponible.

Travailler avec la base de données dans CakePHP est très simple. Nous comprendrons les opérations CRUD (Créer, Lire, Mettre à jour, Supprimer) dans ce chapitre.

Pour travailler sur le téléchargement de fichiers, nous allons utiliser l'assistant de formulaire. Voici un exemple de téléchargement de fichiers.

CakePHP est un framework open source pour PHP. Il vise à faciliter grandement le développement, le déploiement et la maintenance d'applications. CakePHP est basé sur une architecture de type MVC à la fois puissante et facile à appréhender. Modèles, vues et contrôleurs gu

Le validateur peut être créé en ajoutant les deux lignes suivantes dans le contrôleur.

Se connecter à CakePHP est une tâche très simple. Il vous suffit d'utiliser une seule fonction. Vous pouvez enregistrer les erreurs, les exceptions, les activités des utilisateurs, les actions entreprises par les utilisateurs, pour tout processus en arrière-plan comme cronjob. La journalisation des données dans CakePHP est facile. La fonction log() est fournie

Visual Studio Code, également connu sous le nom de VS Code, est un éditeur de code source gratuit – ou environnement de développement intégré (IDE) – disponible pour tous les principaux systèmes d'exploitation. Avec une large collection d'extensions pour de nombreux langages de programmation, VS Code peut être c
