How to build API Services in ThinkPHP
This article will explain in detail how to build API services in ThinkPHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1 download Composer
Composer is a tool that PHP uses to manage dependency relationships.
1.1 windows version
Download address: getcomposer.org/
If an error is reported:
Program Output: PHP Deprecated: Directive 'track_errors' is deprecated in Unknown on line 0
Then modify php.ini:
; track_errors = On (On changed to Off) track_errors = Off
After restarting the HTTP service, the installation passed.
1.2 macOS version
Execute:
Curl-sS https://getcomposer.org/installer | php
If you report the following information incorrectly, or if you can't finish downloading it:
Failed to decode zlib stream
Just go directly to getcomposer.org/download/ to download the latest version of composer.phar.
After downloading, execute it in the directory where composer.phar is stored:
Mv composer.phar / usr/local/bin/composer
Then you can use composer globally, and execute the following command to check the version number:
Composer-v
2 install / upgrade ThinkPHP6
Execute the following command to switch to Aliyun image to accelerate download:
Composer config-g repo.packagist composer https://mirrors.aliyun.com/composer/
Select the directory and execute:
Composer create-project topthink/think projectName
After installation, enter the project directory and execute:
Php think run
Browser access:
Http://localhost:8000/
If the port is changed, execute:
Php think run-p 80
Upgrade ThinkPHP6, enter the project root directory, and execute:
Composer update
In the actual deployment, you should bind the domain name to access the public directory and make sure that other directories are not under the WEB directory.
3 configure debug mode
Rename .example.env to .env in the root directory and set the following code:
APP_DEBUG = true
4 Multi-application deployment
Directory structure
/ www WEB deployment directory (or subdirectory) ├─ / app application directory │ ├─ / myApp subapplication directory │ │ ├─ common.php subapplication function file │ │ ├─ / controller subapplication controller directory │ │ ├─ Index.php subapplication controller │ │ ├─ / model Subapplication model directory │ │ ├─ / view subapplication view directory │ │ ├─ / config subapplication configuration directory │ │ ├─ / route subapplication routing directory │ │ └─. Sub-application more class library directory │ │ | ├─ BaseController.php default basic controller class │ ├─ common.php common function file │ ├─ event.php event definition file | ├─ ExceptionHandle.php application exception definition file (be sure to keep this! Otherwise ERROR 500) | | ─ middleware.php global middleware definition file │ ├─ provider.php service provides definition file | └─ Request.php application request object (be sure to keep this! Otherwise ERROR 500)
Multi-application mode extension think-multi-app
To use multi-application mode, you need to install think-multi-app, which is installed by executing the following command from the project root:
Composer require topthink/think-multi-app
Modify the path of the controller
Open app/myApp/controller/Index.php and adjust namespace
-namespace app\ controller; + namespace app\ myApp\ controller; use app\ BaseController
It can then be accessed through the http service:
Http://127.0.0.1/thinkphp6/public/index.php/myApp
URL rewriting
If you want to omit index.php, you can access the
Http://127.0.0.1/thinkphp6/public/myApp
Add to public/.htaccess:
Options + FollowSymlinks-Multiviews RewriteEngine On RewriteCond% {REQUEST_FILENAME}!-d RewriteCond% {REQUEST_FILENAME}!-f RewriteRule ^ (. *) $index.php/$1 [QSA,PT,L]
URL rewriting has been completed through the official default installation of composer. Here is a memo only.
5 multi-stage controller
The directory structure is as follows:
├─ / app application directory │ ├─ / myApp subapplication directory │ │ ├─ / controller subapplication controller directory │ │ ├─ / api secondary controller directory │ │ ├─ / Login.php secondary controller
Login.php Code: