I have developed the RESTFul API of the WeChat applet before, using the framework ThinkPHP5.0.24. I recently want to upgrade to ThinkPHP6.0.2. The directory structure is: --app --api --controller --v1 --Index. The PHP version written into the route can also be written directly in the following format: Route::get('api/:version/home','api/:version.Index/home'); which is no longer supported in ThinkPHP6.0.2, please refer to https ://blog.csdn.net/qq_35422558/article/details/100563723. In this article, create an api folder under the route file in the root directory of the website. Below is a route.php routing file with a structure such as --route --api --route.php app.php--verdor writes Route::get('api/:version/home','api/:version.Index/home') in api\route.php. [b]Invalid after testing[/b].
By consulting the ThinkPHP6 development manual, it has been clearly pointed out that routing addresses no longer support modules/controllers/operations, but still support controllers/operations or class methods. So there are three solutions
The first one: the method defined to the class can be written as Route::get(':version/home','\app\api\controller\:version\index@home'), if Written in api\route\route.php, the access URL is http://localhost/[b]api[/b]/v1/home. The route definition under the application should add the application name. If it is written in route\app.php, the access URL is http://localhost/v1/home. Remember not to add anything extra.
Second type: The definition to the controller/operation can only be written in the application directory \route\route.php. After testing, the route.php file name can be defined arbitrarily. It cannot be defined in route\api\route.php, and the test is invalid. Route::get(':version/home',:version.Index/home') removes the application name part when defining it, and adds it when accessing the URL, that is, http://localhost/api/v1/home
The third type: use multi-level controller in single application mode --app --controller --api --v1 --Index.php --admin --Index.php --home --Index.php routing at this time Defined in Route\app.phpRoute::get('api/:version/home',api.:version.Index/home')
This problem has not been mentioned in many ThinkPHP6 tutorials. Many online searches have been unsuccessful, so the summary is as follows: 1. The single-application mode multi-level controller uses the api and v1 directories as subdirectories and grandchild directories of the controller. The structure is as above. If it is just one project, I feel this is more reasonable. In this way, both admin and home can use the same view and model.
2. In multi-application mode, you must first install the multi-application mode extension, composer require topthink/think-multi-app, and then delete the controller folder. PHP think build api generates the application directory. There is no need to set auto_multi_app=true. The framework is judged based on whether there is a controller. Whether it is a single application or multiple applications, create a route directory in the application directory and the file route.php in the directory. Define the route in it. There is no need for an application name. Just add it to the URL when accessed. Route::get(':version/home',:version.Index/home') corresponds to url: http://localhost/api/v1/home
3. Route to class method. This method is not commonly used. But as analyzed above, it can be defined in api\route\route.php and route\app.php, but the accessed URLs are different. The former must be added with the application name api, and the latter must be whatever it is. Do not add anything.
4. If it prompts No input file specified when entering the above URL. This problem can be solved by adding index.php to http://localhost/index.php. The suggestion is to add ? after the index.php of RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] in public\.htaccess, which is RewriteRule ^(.*)$ index.php?/$1 [ QSA,PT,L]