One of the most notable features of Yii 2.0 is the introduction of namespaces, so the introduction of custom classes is different. This article discusses how to use the automatic loading mechanism of YII 2.0 to introduce custom classes and namespaces to the system. The purpose of this article is to introduce, if there is an improper understanding please correct me, welcome everyone to share their own methods.
The class we want to be introduced should reach two points:
- In the app, enter the code where you want to use the class name or namespace instead of explicitly calling require ()/include ().
- With Yii's autoloader, it is only loaded when the class is called to follow the principle of yii on-demand loading, saving resources.
We use the Yii 2.0 base template as the demo environment, and the project root directory basic
is named ( /
which is written later), which is the root directory structure:
basic├── assets├── commands├── config├── controllers├── mail├── models├── runtime├── tests├── vendor├── views└── web
Loads the custom class I. Defining class Files
Create a directory /libs
and set up file freedom.php.
<?phpclass Freedom{ public static function yell() { echo "I am FREE!"; }}
II. To
Yii::$classMap
Add Mappings
Open the configuration file /config/web.php
and add a class map to the property at the head of the file [[Yii::$classMap]]
.
<?php...Yii::$classMap[‘Freedom‘] = ‘@app/libs/Freedom.php‘;...$config = [...];return $config;
Note: do not [[Yii::$classMap]]
use =
Direct assignment, because some of Yii's core class mappings are defined in this attribute, and direct assignment can cause these mappings to be lost and yii autoloader not be loaded into the core class. No Zuo no die don ' t try.
Iii. using a custom class
Witness the moment of miracles. Try calling this class in the system, we use Sitecontroller::actionindex () as an example.
<?php...use Freedom; // 别忘了导入这个类,或者在后面调用的时候使用"\Freedom"。class SiteController extends Controller ... public function actionIndex() { Freedom::yell(); }}
OK, let's refresh and try.
Iv. If you still care why
So now we're going to need to introduce [[Yii::$classMap]]
a hair. This is actually an associative array, the array key is "the full permission class name minus the leading backslash", and the corresponding value is the file path that defines the class, where the file path supports the path alias . When calling to a class that has not yet been loaded in code, YII's Audoloader scans this property for the class file name that needs to be loaded.
So, we add the class that we just defined to this mapping array, it can be loaded by yii delay. In fact, we can add this mapping anywhere, as long as the target class is called. The application's master profile is an ideal location because the configuration file is loaded after yii.php, where you can access the Yii
class (interested students can go to the Portal script ), and the configuration data can be centralized in one file.
In addition, because the class we define is under the root namespace, the full-permission class name minus the leading backslash is only left Freedom
. If your class uses a namespace, simply write the fully qualified name in the array key (e.g. [' Custom\classes\freedom ']).
Load the entire namespace
Sometimes we need to write a set of interrelated classes, and if these classes have dependencies, configure the mappings for each class as above ... Non (Jue) (b) Non (GAO) (SI) (ren). If you are following the PSR-4 standard when you define a class under a namespace, we can introduce the entire namespace at once.
The property we're going to use this time is [[\yii\base\Application::$aliases]]
. It is also an associative array that maps a path alias to a directory or another path alias that already exists. Where the array key is the alias to be specified, and the corresponding value is the destination path.
We just need to create a namespace alias and map it to the root directory of all the classes under this namespace. Of course, this root directory below the file structure and class definition to follow PSR-4, otherwise autoloader cannot find the corresponding file.
Try it:
I. Define namespaces and class files
We decided to /libs/vendors
define a set of classes in the directory that are organized in a namespace, with the root directory named free-classes
, all of which are under the namespace free_classes
. Note that I deliberately inconsistent the root name with the root namespace name to indicate that the mapping root is not necessarily the same name as the namespace.
Create /libs/vendors/free-classes/persons/Slave.php
a file, without a directory, create it yourself.
<?phpnamespace free_classes\persons;class Slave{ public static function isFree() { var_dump("I‘m FREE now, thank you!"); }}
Create a file /libs/vendors/free-classes/vehicles/cars/Porsche.php
.
<?phpnamespace free_classes\vehicles\cars;class Porsche{ public static function isFree() { var_dump(‘Are you kidding?!‘); }}
Note: free-classes
The following directory names and structures are subject to the PSR-4 standard.
II. Configuration
[[\yii\web\Application::$aliases]]
To say here, if our namespace is namespace\subnamespace
, then we should set the path alias is @namespace/subnamespace
(detailed reference PSR-4).
Open the configuration file /config/web.php
and configure the application Aliaes property:
<?phpYii::$classMap[‘Freedom‘] = ‘@app/libs/Freedom.php‘;...$config = [ ‘id‘ => ‘basic‘, ... ‘aliases‘ => [ ‘@free_classes‘ => ‘@app/libs/vendors/free-classes‘ ], ...];return $config;
Iii. using classes under a namespace
To witness the miracle again, or to choose in Sitecontroller::actionindex ().
<?php...use free_classes\persons\Slave; // 还是别忘了导入use free_classes\vehicles\cars\Porsche;class SiteController extends Controller ... public function actionIndex() { // Freedom::yell(); Slave::isFree(); Porsche::isFree(); }}
Refresh;-)
[Yii2.0] Loading custom classes or namespaces in YII 2.0 style [configuration using Yii2 Autoloader]