Reading time: 10 minutes

Technical preparation: Basic PHP syntax

This chapter focuses on the basics of ThinkPHP and the MVC structure commonly used in Web server development.

I. Import documents

Remember how we exported Helloworld in the previous chapter?

Yes, you typed http://localhost/tp5_demo/public/ into your browser

PHP /tp5_demo/public/index.php file in htdocs folder.

When we type http://localhost, Apache accesses the DocumentRoot path specified in its httpd.conf file, which in Zhao’s case is E:/xampp7/htdocs.

/tp5_demo/public = /tp5_demo/public = /tp5_demo/public

So why did you end up in the index.php file? This is because the httpd.conf file has another attribute that defines DirectoryIndex, which defines index.php, index.html, etc as entry files. (what? Why do you think there’s no picture? Go to your httpd.conf file)

In ThinkPHP, if you don’t specify an access address, the default route is to the index() method in the Index controller under the Index module.

In other words, input

http://localhost/tp5_demo/public/

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

It has the same effect.

Ii. Controller

In an MVC architecture, controllers typically only do the following:

  • Parameter calibration
  • Call the service
  • Invocation model
  • The output

To create a controller in the ThinkPHP framework, create a new class in the Controller module of the corresponding module.

The figure shows that the INDEX controller is set up in the INDEX module.

The Index () method under the Index controller represents an Action, which in API development is also an interface, and can be bound by configuring the framework’s routing URL.

It is important to note that there should not be too many logical operations in the Action. The Action should only be a caller in the Model layer and the View layer. The actual logical operations should be handled in the Model layer, and the more complex logic should be handled in the Service layer. This layered idea is known as section-oriented programming (AOP).

The advantage of this is that when a common operation (such as user login, calculating the amount of activity) changes, you only need to change the code in the model without affecting the controller code that calls the model.

If you put all the logic in the controller, you’ll need to change all the associated controller code whenever you need to change the code.

Zhao tong Shoes has seen all the logic in the controller, no model of the project, when writing do not need to consider stratification, do not need to consider iteration, cool very much.

But when requirements change, maintenance programmers just want to have sex.

Iii. Model

The model definition is typically one model class for one table.

Most of the logical operation in a server-side development is in the Select/Create/Update/Delete all kinds of data table.

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

The ThinkPHP framework provides many convenient features for models, such as hiding fields, field modifiers, paging queries, and defining associations.

We’ll cover the model and these features in more detail later.

Iv. View

The view layer outputs the corresponding HTML code.

In the framework, you can use a template engine (ThinkPHP built-in engine, Smarty engine) for variable substitution.

But now the more popular server-side API development, through API data transfer and front-end interaction, less use of view for page rendering.

5. Route

Routing is available in many PHP frameworks, even in front-end frameworks such as Vue and React.

Routing is essentially 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 try adding hello() to the Index controller:

To access the URL: http://localhost/tp5_demo/public/index/index/hello

As you can see, the output is successful

“Here is hello Action”.

What this URL actually means is to access the Hello method in the Index controller under the index module.

Routing has many other powerful features that we will cover in subsequent chapters.

Six, summarized

In this chapter, we introduced the basic uses of three popular MVC components, as well as the concept of routing.

Kids should have a rough idea of the workflow of a back-end interface, namely: Routing -> Controller -> Model (-> View).

The actual use of each component will be described in detail at the beginning of the next section.

Appendix 1: Hide the index. PHP file

Have you noticed a bug in this article?

That’s right, as we said in the entry file, when going to http://localhost/tp5_demo/public/

Apache will automatically access the index.php file in the public directory.

But why visit http://localhost/tp5_demo/public/index/index/hello

You don’t need to include this file in the URL?

We can find a.htaccess file in the public directory, which helps us hide index.php.

Now mask the code and restart Apache.

Once again, visit http://localhost/tp5_demo/public/index/index/index.

The browser will return 404.

And if we go to http://localhost/tp5_demo/public/index.php/index/index/index.

The page successfully outputs Helloworld.

Appendix II. Virtual domain name

After knowing the entry file rule, do you find it troublesome to enter the project path every time you access the interface? Let’s next configure a local virtual domain.

First edit the httpd-vhosts.conf file in the Apache directory and enter the following code:

Then edit our local hosts file, Windows10 directory is:

C:/Windows/System32/drivers/etc/hosts

Enter the following code:

127.0.0.1 tp5.com

Restart Apache on xamPP and type tp5.com in the browser. If you can see Helloworld again, it means that the configuration is successful. (If there is a problem with the configuration of the children’s shoes, please leave a message to Zhao in the background of the public account.)

After the configuration is completed, we only need to access the address tp5.com in the future to directly access the bound project path.

—– End —–

More oliver

Please scan the qr code below

Welcome to pay attention ~