Yii2 frame integral structure

Yii2 framework is a very large but not bloated php framework. Use Yii2 framework, can greatly enhance the development efficiency, but all pros and cons, we are developing at the same time enjoy the convenience brought by Yii2 framework must assume that brought us the pit.
To uphold the should know these know why they thought it took a week's time, I looked linuor of "in-depth understanding Yii2.0"
According to my understanding of what sort of overall structure Yii2 framework. Since then referred to the framework.

Yii Framework base

Three basic concepts of this framework:

  • Attributes
  • event
  • behavior

After all, three things.

Attributes

Saw the word might be confused, then what is it property.
For example, you are now playing a role-playing game, there is a role both hands 十方大剑, a 十方盾牌.
Well, from an object-oriented point of view, you need to have character classes 左手武器and 右手武器two member variables.
And from the role of class 左手武器and 右手武器gain strength properties.

That is to say the concept of property, it is easy to understand.

event

Or in role-playing games, for example (sorry not here to play games with friends, and forgive my lack of imagination, or if you have a good analogy I can tell), which have the mandate and the game system, and the task will be divided into the main quest and side quests, doing the main quest is normally trigger a series of quests. Trigger these quests can be considered to be a series of events.

behavior

Behavior of the frame can also be used to role-playing games, for example, you picked up a magic book in the endless wilderness, I learned Jinzhou magic 召唤神龙.
So you put a program change, add to your class a method call magic, which is impossible. We have to make your class has a method for adding dynamic functionality, so that all people get the magic book or limited professional who can learn this behavior is the role of the Jinzhou, dynamic method to increase class.

Above, with the weapons of the three framework to achieve, we can build a more rapid and flexible upper program.

Design Patterns

Dependency Injection

Frame now the most common mode world above, MVC model, specific details are not set forth, now online introduction to MVC can be said to be bad streets.
MVC design pattern but only on a broad framework, the core idea is layered, the ultimate aim of decoupling. MVC framework based on the application of a lot of classic design patterns and design patterns later developed.

The most important thing is:

  • Dependency Injection
  • Service Locator.

So what is dependency injection it
first to a period of no dependency injection code:

<?php
// 这段代码将 db1 中的t1表的数据备份到 db2 库的 t2 中。
// 所使用的变量都在逻辑过程中申请。
class Archive {
    public function doArchive() {
        $dataDB = new DB1();
        
        $data = $dataDB->query("select * from t1");

        $backDB = new DB2();

        foreach ($data as $key => $value) {
            $backDB->query("insert into t2 values ". implode(',', $value));
        }

        die("备份完成");
    }
}

If there's a demand for it, let you back up data to db4 db3, the same logic, then how can you do it?
Friends say the proper way, that's not easy, copy & paste, buttoned.
So, you have one more method or a class.
When all the way to your methods and classes to more than 10, you will not have any feeling.
Until now, said product demand has changed, so you have all the backup library have changed db3, then you should work hand stopped, then backed up the code library that changed db3.

And, if you are using a static language, then you should re-compile the code again. About static long-language compilation: Why it takes so long to compile c

Then there is this following:

class ArchiveNew {
    private $originDb;
    private $backDb;

    public function doArchive() {
        $dataDB = new $this->originDb();
        
        $data = $dataDB->query("select * from t1");

        $backDB = new $this->backDb();

        foreach ($data as $key => $value) {
            $backDB->query("insert into t2 values ". implode(',', $value));
        }

        die("备份完成");
    }

    /**
     * @param mixed $originDb
     */
    public function setOriginDb($originDb)
    {
        $this->originDb = $originDb;
    }

    /**
     * @param mixed $backDb
     */
    public function setBackDb($backDb)
    {
        $this->backDb = $backDb;
    }
}

This method, need to use all the databases are put outside to manage, then we say that is dependent on several databases, said the behavior of the external database is set to dependency injection.
This is just a very primitive use, you can continue to extend the external dependencies into a unified place to manage, then there is injected into the container (di Container)

Service Locator

Service locator like a registry, a service to sign up for a service locator, you can use a name out of this service from the service locator.

$locator = new ServiceLocator;
$locator->set('a');
$locator->get('a');

Service Locator is based on dependency injection, at the time of access to services, will actually sign up for a service in the container.

Request and response

In fact, this section is mainly about the request.
The requested content will be more, but also to knowledge of network protocols, not to undertake here.
List what important points:

  1. Landscaping route (the route to modify the original address more beautiful, see FIG request guide portion)
  2. url parsing (beautify the route resolution to the original request)
  3. Request management (including a request header, the request body, resolver, etc.)

database

Although this figure somewhat funny, but no doubt the importance of database (MySQL) for PHP.

Type Conversion

For compatibility with various databases frame, the data types do multilayer encapsulation, and made a series of conversion rules:

Affairs

Framework supports nested transactions, but must appear in pairs nested transactions (note !!!).
See matters in this section, when a colleague happened to a bug, the circulation of the script, did not commit an error after or rollback, lead to the next generation of affairs have become this transaction sub-transactions. The nested transaction framework, in fact, use the code simulation, if the parent did not submit the transaction, then the child will never submit the transaction.

event

const EVENT_INIT = 'init';                      // 初始化对象时触发
const EVENT_AFTER_FIND = 'afterFind';           // 执行查询结束时触发
const EVENT_BEFORE_INSERT = 'beforeInsert';     // 插入结束时触发
const EVENT_AFTER_INSERT = 'afterInsert';       // 插入之前触发
const EVENT_BEFORE_UPDATE = 'beforeUpdate';     // 更新记录前触发
const EVENT_AFTER_UPDATE = 'afterUpdate';       // 更新记录后触发
const EVENT_BEFORE_DELETE = 'beforeDelete';     // 删除记录前触发
const EVENT_AFTER_DELETE = 'afterDelete';       // 删除记录后触发

These events and other events is no different, all will be executed at a certain time, not one elaborated.

Optimistic and pessimistic locking

Framework comes with the realization optimistic locking, if there are similar needs, you can reload yii \ db \ ActiveRecord :: optimisticLock ( ) method returns the version number field to the database. When updating and deletion, the framework will do the appropriate action to ensure that updated data of their own to get the data, rather than being someone else modified.
Because the pessimistic locking does not apply to web applications, so the framework does not implement pessimistic locking.

to sum up

Yii2 overall architecture framework than 1 version raised a force grid. Thanks to the characteristics and the namespace, many cleaning frame structure.
For programmers do not want to understand the underlying framework to achieve, can only understand the basic use, it can be more than hand-written business code, without affecting your development efficiency.
Only when you meet inside a very boring frame error or a bug, you will think, I go, so that how boring, how the framework will be a bug, but very often environmental, configuration, or a specific code causes of. If you do not understand the logic of the internal framework, this error or bug will delay you for a long time.

Guess you like

Origin www.cnblogs.com/wdy1184/p/11567954.html