Home > PHP Framework > ThinkPHP > What you must know about the ThinkPHP framework

What you must know about the ThinkPHP framework

silencement
Release: 2019-11-27 18:20:35
forward
4448 people have browsed it

What you must know about the ThinkPHP framework

In this chapter we mainly explain the basic overview of ThinkPHP and the MVC structure commonly used in Web server development.

1. Entry file

Enter the URL http://localhost/tp5_demo/public/

in the browser. To access the /tp5_demo/public/index.php file in the htdocs folder, why is this? Let’s break it down step by step.

When we enter http://localhost, Apache will access the DocumentRoot path specified under its httpd.conf file, which is E:/xampp7/htdocs in Zhao Tongxie.

The splicing of /tp5_demo/public at the end means accessing the /tp5_demo/public folder under the DocumentRoot path.

So why is the index.php file ultimately located? This is because there is another attribute in the httpd.conf file that defines DirectoryIndex, which defines index.php, index.html and other files as entry files. (What? Why are you saying there is no picture? Look through your httpd.conf file yourself)

In ThinkPHP, if the access address is not specified, it will be routed to the Index controller under the index module by default. index() method.

That is, enter

http://localhost/tp5_demo/public/

http://localhost/tp5_demo/public/index/index/index

has the same effect.

2. Controller (Controller)

In the MVC architecture, the controller is generally only responsible for the following functions:

Parameter verification

Calling service

Calling model

Output result

To establish a controller in the ThinkPHP framework, you only need to create a new class in the controller module in the corresponding module.

What you must know about the ThinkPHP framework

As shown in the picture above, the Index controller is established in the index module.

The index() method under the Index controller represents an Action, which is also an interface in API development and can be bound through the routing URL of the configuration framework.

It should be noted that there should not be too many logical operations in Action. Action should only be a caller of the model layer (Model) and the view layer (View). The actual logical operation should be placed Go to the model layer for processing, and more complex logic should be placed in the service layer (Service) for processing. This layered idea is the so-called aspect-oriented programming (AOP).

The advantage of this is that when a common operation (such as user login, calculation of activity amount) changes, you only need to modify the code in the model, and it will not affect the controller code that calls the model.

If the logic is placed in the controller, then once the code needs to be modified, all associated controller codes must be modified.

I have seen projects that put all logic in the controller and do not use models at all. There is no need to consider layering or iteration when writing, which is very refreshing.

3. Model

The model definition is generally a model class corresponding to a data table.

Most of the logical operations in server-side development are actually Select/Create/Update/Delete various data tables.

Some simple logical operations, such as: getting the article data with ID 75. It should be encapsulated as a method in the model and called at the controller layer.

ThinkPHP framework provides many convenient functions for models, such as: hidden fields, field modifiers, paging queries, and defining relationships.

We will introduce the model and these functions in detail later.

4. View

The view layer outputs the corresponding HTML code.

You can use template engines (such as ThinkPHP's own engine, Smarty engine) to perform variable substitution in the framework.

But nowadays, API development is more popular on the server side. Data is transmitted through API to interact with the front end, and views are rarely used for page rendering.

5. Routing (Route)

Many PHP frameworks have the routing function, and routing has even been added to front-end frameworks such as Vue and React. this concept.

Routing is actually a mapping between our access URL and the Action in the controller, as mentioned above:

http://localhost/tp5_demo/public/index/index/ index

is to access the index() method under the Index controller in the index module.

We can give it a try and add the hello() method to the Index controller:

What you must know about the ThinkPHP framework

##6. Summary

In this chapter we introduce Understand the basic uses of the three major components of MVC that are now popular, as well as the concept of routing.

Children should have roughly mastered the workflow of a back-end interface, that is: routing->controller->model (->view).

The above is the detailed content of What you must know about the ThinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template