Yii2 development API elementary course

Time:2021-7-21

brief introduction

useyii2conductAPIDevelopment, the goal of this document is to run through the whole process in the simplest way, not tangled with many details.

Core concepts

  1. Entry script

  2. application

  3. Application component

  4. controller

  5. action

  6. Model

Request process

Entry script > route parsing > controller > model (data processing) - > response

Entry script

Simply understand, the entry script isindex.php, in configurationWeb serverThe request needs to be directedindex.phpWhere the folder is located, in theyii2In the framework, that iswebfolder.

application

for instance,yii2The application in the framework is a bit similar to the role of manager in the factory. It plays the role of management and adjustment, which is also the function provided by the application (the application here refers to an application instance by default). What is the application of this manager management adjustment? This is the following application component.

Application component

yii2Application components are some classes that the framework has encapsulated to provide specific functions, such asdb,log

These components are in theyii2It can be customized and configurable.

Customization means that you can write your own function class and configure it as an application component in the framework. Configurable means flexible, available or not.

stayyii2Where is the application component configuration in?

Look at the configuration file (if you are taking over the project, there may be many custom configuration files that are slightly different from the official version. The description in this article is all based on the official version)web.phpIncomponentscomponentsEach key value pair in therequest,cache,user,errorHandler,mailer,log,db,urlManagerAnd so on.

How to use it in logic?

\Yii::$app->dbIn this way, the corresponding application components can be obtained. Obviously, thedbIt can be replaced by other application components, such asrequestAnd so on.

controller

After the entry script receives the request,URIIt will be processed by the routing manager and resolvedcontrollerandactionThese two most important things, with these two values, we can locate the execution logic corresponding to the request. The controller is stored in the root directory of the project (the root directory of the project is not the directory of index. PHP, but the directory of the whole project)controllersFolder, the naming format isBig hump + big hump

action

In understanding, an action is a method defined in a class,yii2There are two kinds of methods in the controller: action and common method.

There is a requirement for the naming of actions, and there must beactionPrefixes, such asactionViewThat’s it. The common method is the common method.

Model

yii2Generally speaking, model refers to data model, which is used to interact with database.

yii2The main difference between the two types of models in is what the inherited parent class is.modelThe storage directory of is under the project root directorymodels

class TestEntity extends \yii\base\Model

The difference between the two models is that the common model (non AR model) is generally used to load data and does not interact with the database.

What’s the meaning of this?

For example, you create a user table with fieldsid,name,ageSo if you create a normal modelUserIt’s usually like the following. It’s usually calledxxxEntity

class UserEntity extends \yii\base\Model

Therefore, the common model is actually a shelf, which is generally used to receive the request data. According to the actual situation, select the corresponding common model and use the data carried in the requestsetMethod to fill in the shelf, it becomes like this

class UserEntity extends \yii\base\Model

{

protected $id = 1;

protected $name = 'lovan';

protected $age = '24';

...

}

After this treatmentEntityWill be passed in as a parameterARIn the model, theARModel to take over the next stepCRUDRelated operations.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Wild ginseng in the city

Recommended Today

React Series 7: parent-child component communication

preface In this section, we will introduceReactThe method of parent-child component communication in, the method of parent-child communication and child-parent communication. This article will introduce you to the following: Learn the nesting of components Parent component communicates with child component The child component communicates with the parent component Learn the nesting of components Let’s recall […]