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

How to build back-end api Interface for thinkphp6

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to build a back-end api interface in thinkphp6". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Download tp6

I use the integrated environment phpstuday, installed composer, through the composer installation tp6,thinkphp official website no longer supports direct download.

Composer create-project topthink/think tp6

You can also install tp6 directly by following the steps of tp6 viewing cloud documentation.

Enter through the cmd command window in the downloaded tp6 directory

Php think run

Type 127.0.0.1Jap8000 into the browser and visit the following page to install it successfully.

2. Turn on error debugging

In the beginning, let's turn on the error debugging of tp6

1. Find the show_error_msg under config/app.php and change it to true

two。 Find the .example.env file in the root directory below, rename the file, and delete .example

If you look at the code inside, you will find that it turns on app_debug debugging

In this way, we can see the complete error message, such as:

3. Hide the entry file

In section 1, we visit

Http://127.0.0.1:8000

What is actually visited is

Http://127.0.0.1:8000/index.php/index/index

You can also visit it this way.

Http://127.0.0.1:8000/index/index

If nothing is entered, the index controller is accessed by default. This is defined in the config/app.php file, and you can also modify the default controller.

Also, regardless of accessing any controller, if no method is filled, it accesses the index method in the controller, and if the index method does not exist, it prompts the error message-the method does not exist.

Tp6 helps us hide the entry file by running the web service enabled by php think run in the root directory of the project, so you can access it in a third way. But what we are going to talk about in this section is how to hide the entry, how can you use the web service that comes with tp6. So do it yourself.

When we develop, we often build a set of web solutions such as WNMP locally, which requires us to hide the entry file index.php by ourselves.

Why hide the entry file?

Because of the http://127.0.0.1:4321/index.php/index/index access method like this, this index.php is very ugly.

Superfluous.

DANGER

Because of the integrated environment I used here, I chose the apache server, so I only found the way to hide the entry files in apache. Nginx needs to search on its own.

Now I enable the apache server, and the open port is 4321

When I want to pass

Http://127.0.0.1:4321/index/index

Access failed when you went to access the method

When I added access to the entry file, the access was successful.

Http://127.0.0.1:4321/index.php/index/index

Hiding index.php is easy to implement, just find the .htaccess file in the public directory and add the following code.

# if the mode_rewrite.c module exists, execute the following command: Options + FollowSymlinks-Multiviews RewriteEngine On # enable rewriteEngine #!-d is not a directory or directory does not exist RewriteCond% {REQUEST_FILENAME}!-d #!-f is not a file or file does not exist RewriteCond% {REQUEST_FILENAME}!-f RewriteRule ^ (. *) $index.php [QSA,PT L] # Parameter explanation # ^ (. *) $: matches all intersection mappings # QSA: (Query String Appending) indicates that reserved parameters are passed into get? Xxx==xx; # PT: leave this URL to Apache for processing; # L: as the last rule, this rule will no longer match this one

Visit now

Http://127.0.0.1:4321/index/index

Access successful

It should be noted that in the first section, we saw that after running php think run, our project directory accessed the public directory

The official document also says that only the public directory should be accessible to the outside world in the project, so if you have any pictures, videos and other resources that need to be accessed, you should put them in this directory.

4. Solve cross-domain problems

In application development, the front and back ends are developed separately, and the front and back ends usually build a web service and run on different ports. When the front end accesses the interface of the back end, it will report a cross-domain error. However, this cross-domain problem usually needs to be dealt with by a backend. It is very convenient for tp6 to have a special middleware to do this. You only need to add the middleware to the middleware.php under the app directory to achieve cross-domain access.

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: 270

*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