Understanding Yii2 from the perspective of configuration files

foreword

Yii2 is a peculiar framework that sacrifices the popular decoupling design, and uses a highly coupled structure to provide developers with a convenient abstraction of several classes, Application, Module, Component, and even ServiceLocator. Which class do you want the object to have? The feature directly inherits the corresponding object, and if you want to change it, you need to overwrite the method of the parent class. Although it does not conform to the open and closed principle, the single responsibility principle, the Li's replacement principle and other object-oriented design principles, it is indeed very convenient.

This article starts from the configuration file of the Yii2 Advance version, and introduces the Yii2 framework from the perspective of the configuration file in detail. Some of the kernel aspects involved will be explained one by one in the subsequent articles.

The configuration file of Yii2 is divided into two levels:

  1. common level
  2. site level

Common, as the name suggests, is a common configuration file. The site level will overwrite the common level configuration, and the scope is also the site.

0. config/bootstrap.php

In order to simplify the development of Yii2, all user-defined directory loading methods use the autoload method defined by Yii2 (autoload in BaseYii.php registered in Yii.php), instead of editing composer.json and then dumping autoload.

The usage is to register all paths in index.php to the global container Yii. The method is Yii::setAlias(); method.

The editing place is determined in this bootstrap.php file, but this stage is not Yii's bootstrap stage, it is only triggered in index.php. There is ambiguity, which needs to be paid attention to.

In the common level, the root of the site is set. In the site, the root of the module is set.

1. config/main.php

There are many settings in main.

0. 'params'

First require all params.php (params-loacl.php) including common and this site, and put the return value under the params key.

1. 'id'

is the name of the current site

2. 'basePath'

As the name implies, the site root directory

3. 'controllerNamespace'

The namespace of the controller under the site (not in the module), in the MVC stage, Yii2.0 will automatically generate the namespace of the controller based on this attribute combined with routing. The controllerNamespace in the controller at the module level needs to be redefined, which can be defined in the init( of Module.php ( ) method is assigned directly.

4. 'bootstrap'

This configuration is a bit interesting. Unlike bootstrap.php, this is the definition that really acts on the bootstrap stage. The method is to write the module or component or even any object that you need to respond to in the bootstrap stage.

What is the response? There are two cases, if your module or component or even an obscure object references the yii\base\BootstrapInterface interface and implements the bootstrap() method. This method will be called directly. If there is no reference, then Just instantiate it and store it in the object container.

Since bootstrap is mentioned, at this stage Yii2 will also call the extension used to do the same thing as above. If you write an extension about Yii2 yourself, it will be instantiated and cached in Yii's object container at this time. As for the extension's Writing is very simple, just add a bootstrap entry that can be used by Yii2 for your extension (an object, refer to the yii\base\BootstrapInterface interface, and configure it in the extension's configuration file vendor\yiisoft\extensions.php ).

5. 'modules'

The namespace of the Module object in the corresponding modules (such as backend) directory. It is convenient for Yii2 to enter the module you define.

module is a set of MVC encapsulation in Yii2, this concept may be different in other frameworks, such as laravel.

6. 'component'

In fact, this thing, called module in ZendFramework 3.0, is the realization of a function, such as permission management, routing management, filters and so on.

In Yii2, the configuration of the 'class' field is built into some components. That is to say, as a developer, you don't need to know where the object corresponding to this component is, and you can use it directly after configuration. Of course, if you write it yourself, you must Specifies the object entry for the component.

Like module, component is very closely related to the whole startup process of Yii2. If you want to understand it in detail, you must be clear about its startup process, which will be explained in detail in subsequent articles.

If you want to understand the configuration of component in detail, you must have a clear understanding of Yii2's DI and object model, and follow-up articles will be introduced in detail. The article is here

7. 'Event on...'

This is a bit special, but very useful, you can bind a callable to the default event of the Application (note that other events, such as MVC, due to different instances, the binding here is invalid), and let it trigger at the corresponding period. Yii2 The event management will be described separately.

Summarize

The configuration file of Yii2 is relatively simple, just as the original intention of the author "Yi", everything is so simple.



Author: Vett
Link: https://www.jianshu.com/p/41e200c88779
Source : Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

 
 
G
M
T
 
 
Detect languageAfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu
 
AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu
 
 
 
 
 
 
 
 
 
Text-to-speech function is limited to 200 characters
 
 
Options : History : Feedback : Donate Close

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256009&siteId=291194637