ThinkPHP6 architecture structure


Entry file (index.php)

The PHP file requested by the user, responsible for processing a request (note, not necessarily a URL request) life cycle, the entry file is located under the public directory. The most common entry file is index.php. 6.0 supports multiple application entries. You can add entry files to each application, such as a separate entry file admin.php for background applications. .

If automatic multi-application is turned on, generally only one entry file index.php is needed.

Application(application>

supports multiple applications. Each application is a subdirectory of an app directory. Each application has independent routing and configuration. As well as MVC related files such as controllers and models, these applications can share the framework core and extensions. And can support composer application loading.

Container (container)

ThinkPHP uses (object) containers to uniformly manage object instances and dependency injection.

The work of the container class is completed by the think\Container class, but in most cases we use the application class (think\App class) or It is the app helper function to complete the container operation. All object instances in the container can be called through the container identifier singleton. You can bind an object identifier to the object instance in the container. If there is no binding, use the class name as the container identifier.

System Services

The concept of system services refers to some basic services that need to be relied upon when executing certain components or functions of the framework. Service classes can usually inherit from the system think\Service class, but it is not mandatory.

You can register an object in the system service to the container, or perform related dependency injection on certain objects. Due to the execution priority of system services, you can Ensure that relevant components have completed relevant dependency injection during execution.

Route(route)

Route is used for planning (usually simplified at the same time) ) The requested access address, establish a routing rule => mapping relationship between the access address and the actual operation method.

ThinkPHP is not forced to use routing. If no routing is defined, you can use it directly. Controller/Operation" method. If a route is defined, the routing address corresponding to the route cannot be directly accessed. Once the mandatory routing parameter is turned on, a route must be defined for each request (including the home page).

There is a certain performance loss in using routing, but it is also safer because each route has its own validity conditions. If the request does not meet the conditions, it will be filtered. You are far better than you in the operation of the controller. It is much more practical to make various judgments.

In fact, the role of routing is far from being as simple as URL specification. It can also implement functions such as verification, permissions, parameter binding, and response settings.

Controller

Each application has an independent class library and configuration file. There are multiple controllers under an application responsible for responding to requests, and each controller is actually an independent Controller class.

The controller is mainly responsible for receiving requests, calling related model processing, and finally outputting through the view. Strictly speaking, the controller should not be too involved in business logic processing.

In fact, the controller can be skipped. Through routing, we can directly dispatch the request to a model or other class for processing.

ThinkPHP's controller class is relatively flexible and does not need to inherit any basic class library.

A typical Index controller class (single application mode) is as follows:

<?php
namespace app\controller;

class Index 
{
    public function index()
    {
        return 'hello,thinkphp!';
    }
}

It is generally recommended to inherit a basic controller to facilitate expansion. The system provides an app\BaseController controller class by default.

Operation (action)

A controller contains multiple operations (methods), and the operation method is the smallest unit of URL access.

The following is a typical Index controller operation method definition, which contains two operation methods:

<?php
namespace app\controller;

class Index 
{
    public function index()
    {
        return 'index';
    }
    
    public function hello(string $name)
    {
        return 'Hello,'.$name;
    }
}

The operation method can not use any parameters. If a non-optional parameter is defined, And if it is not an object type, the parameter must be passed in through the user request. If it is a URL request, it is usually passed in through the current request. The parameters of the operation method support dependency injection.

Model

Model classes usually complete the actual business logic and data encapsulation, and return data independent of the format.

The model class does not necessarily need to access the database, and in the architectural design of ThinkPHP, the database connection will only be made when the actual database query operation is performed, which is a true lazy connection.

ThinkPHP’s model layer supports multi-layer design. You can perform more detailed design and division of labor on the model layer, such as dividing the model layer into logical layer/service layer/event layer, etc.

Model classes usually need to inherit the think\Model class. A typical User modeler class is as follows:

<?php
namespace app\model;

use think\Model;

class User extends Model
{
}

View(view)

After the controller calls the model class, the returned data is assembled into output in different formats through the view. The view decides to call the template engine to parse the content and then output it or output it directly according to different needs.

Template engine (template)

Some special template tags can be used in template files, and the parsing of these tags is usually implemented by the template engine.

The new version no longer has a built-in think-template template engine. If you need to use the ThinkPHP official template engine, you need to install the think-view template engine driver extension separately.

Drive

Many components of the system adopt driver design, which can be expanded more flexibly. The location of the driver class is placed in the core class library by default Under the directory, you can also redefine the namespace of the driver class library and change the driver's file location.

The 6.0 version of the driver is installed and managed using Composer.

Middleware

Middleware is mainly used to intercept or filter HTTP requests of applications and perform necessary business processing.

Some core functions of the new version are processed by middleware, which you can flexibly turn off. Including Session function, page Trace function, request caching and multi-language function.

Event

6.0 has used the event mechanism to replace the original behavior and Hook mechanism. You can use the features of the event mechanism in your application to expand functions.

In addition, the callback mechanism for database operations and model operations to complete data operations also uses event mechanisms.

Helper function (helper)

The system provides helper function support for some common operations, but the core framework itself does not rely on any helper function. Using assistant functions has no direct impact on performance, but sometimes you cannot enjoy the convenience of automatic reminders from the IDE. However, whether to use assistant functions depends on the project's own specifications. You can also rewrite the assistant functions provided by the system in the public function file of the application. .