Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of the overall Architecture of thinkPHP5.0 Framework

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you an example analysis of the overall architecture of the thinkPHP5.0 framework, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The details are as follows:

ThinkPHP5.0 applications are organized based on MVC (Model-View-Controller) approach.

MVC is a design pattern that forcibly separates the input, processing, and output of the application. Using MVC applications is divided into three core components: model (M), View (V), and Controller (C), each of which handles its own tasks.

5. 0% of URL access is subject to routing decisions. If the route is turned off or there is no matching route, it is based on:

Http://serverName/index.php (or other application entry file) / module / controller / operation / parameter / value.

Some of the following concepts need to be understood and may be mentioned frequently in later content.

Entry file

The PHP file requested by the user is responsible for handling the life cycle of a request (note, it is not necessarily a URL request). The most common entry file is index.php, and sometimes new entry files are added for some special requirements, such as a separate entry file admin.php for the background module or a controller program entry think.

Application

An application in ThinkPHP is an object that manages the system architecture and lifecycle, which is completed by the\ think\ App class of the system. Applications are usually called and executed in the entry file. Applications with the same application directory (APP_PATH) are considered to be the same application, but an application may have multiple entry files.

The application has its own independent configuration file and public (function) file.

Module

A typical application consists of multiple modules, which are usually a subdirectory under the application directory, each with its own independent configuration files, public files, and class library files.

5.0 supports single module architecture design. If there is only one module under your application, the subdirectory of this module can be omitted and modified in the application configuration file:

'app_multi_module' = > false

Controller

Each module has an independent MVC class library and configuration file. Under one module, there are multiple controllers responsible for responding to requests, and each controller is actually an independent controller class.

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

In fact, controllers can be skipped in 5.0, and through routing we can schedule requests directly to a model or other class for processing.

The controller class of 5.0 is flexible and does not need to inherit any basic class libraries.

A typical Index controller class is as follows:

Namespace app\ index\ controller;class Index {public function index () {return 'hello.Thkphophony;}}

Operation

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

The following is a typical definition of the operation method of an Index controller, including two operation methods:

Namespace app\ index\ controller;class Index {public function index () {return 'index';} public function hello ($name) {return' Hello,'.$name;}}

The action method can use no parameters, and if an optional parameter is defined, it must be passed through a user request, or $_ GET or $_ POST in the case of a URL request.

Model

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

The model class does not have to access the database, and in the 5.0 architecture design, the database connection will be carried out only when the actual database query operation is carried out, which is a real lazy connection.

The model layer of ThinkPHP supports multi-layer design, and you can make a more detailed design and division of the model layer, such as dividing the model layer into logic layer / service layer / event layer and so on.

View

The data returned by the controller after calling the model class is assembled into different formats of output through the view. The view decides whether to call the template engine for content parsing or direct output according to different requirements.

Views usually have a series of template files that correspond to different controllers and methods of operation, and support the dynamic setting of template directories.

Drive

Many components of the system adopt drive design, which can be expanded more flexibly. The location of the driver class is placed under the core class library directory by default, and the namespace of the driver class library can be redefined to change the file location of the driver.

Behavior

Behavior are actions that are performed at a predefined location of the application. Similar to the concept of "aspect" in AOP programming, binding related behavior to a certain aspect has become a kind of AOP programming idea. Therefore, the behavior is usually related to a location, and the execution time of the behavior depends on the location to which it is bound.

To perform a behavior, first listen for the behavior in the application, such as:

/ / listen for behavior\ think\ Hook::listen ('app_init') at app_init location

Then bind the behavior to a location:

/ / bind the behavior to the app_init location\ think\ Hook::add ('app_init','\ app\ index\ behavior\ Test')

If more than one behavior is bound in a location, it is executed in the order of binding, unless an interrupt is encountered.

Namespace

ThinkPHP5 uses the namespace of PHP to design and plan class library files, and conforms to the automatic loading specification of PSR-4.

The above is all the contents of the article "sample Analysis of the overall Architecture of the thinkPHP5.0 Framework". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report