Home > Article > PHP Framework > ThinkPHP5.2: Routing adjustments and improvements
The routing part of ThinkPHP5.2, like other components, has been streamlined and optimized, mainly including the following aspects:
Cancel the return of route definition Array form
Because it is not conducive to route cache generation, the route definition file cancels the method of returning an array to define routes, and the routing method must be used to register the route.
For example:
return [ 'hello/:name' => 'index/hello', ];
must be changed to:
Route::get('hello/:name', 'index/hello');
Routing definition file location for multiple applications
Single application In mode, the route definition file is under the route directory as before. If your project uses multiple applications, the route definition and matching of each application are independent, and there is no concept of modules. The location of the route definition file It should be under the route/application subdirectory, for example:
route/index/route.php // index应用的路由定义文件 route/index/web.php // index应用的第二个路由定义文件 route/admin/route.php // admin应用的路由定义文件
The default URL rule becomes
http://域名/入口文件(或者应用名)/控制器名/操作名
The routing rule of the application is actually the defined entry file ( or the part of the URL after the application name), excluding the application.
Automatic multi-application
The latest version 5.2 can support accessing multiple different applications in the same entry file (previously one must be added for each application Corresponding entry file).
For example, use in the index.php entry file:
(new App())->autoMulti()->run()->send();
You can automatically access multiple applications through URLs without creating an entry file
http://serverName/index.php/admin
If your default application is not index (The default is the entry file name), then you can specify the default application through the name method.
(new App())->autoMulti() ->name('admin') ->run() ->send();
Supports alias mapping of application names, for example:
(new App())->autoMulti([ 'think' => 'admin', // 把admin应用映射为think ])->run()->send();
If you need to customize an application, you can use
(new App())->autoMulti([ 'admin' => function($app) { $app->debug(true)->useClassSuffix(); } ])->run()->send();
to cancel alias routing
Due to limited usage scenarios and performance overhead issues, the original alias routing function is cancelled, and it is recommended to use resource routing or a separate route instead.
Cancel shortcut routing
Because the usage scenarios are limited and do not meet the specifications, the original controller shortcut routing function has been cancelled.
Cancel empty controller and empty operation
The original empty controller and empty operation function has been cancelled. Please use the MISS routing function instead, and it can support different Routing grouping sets up MISS routing. At the same time, the empty_controller configuration is discarded.
Cancel automatic search of controllers
Due to performance reasons, the automatic search function of multi-level controllers for routing has been cancelled. Please clearly specify the route to be routed in the routing rule definition. Multi-level controller.
The routing function is designed independently
The routing function is no longer fixedly executed, and is designed to be a response monitor for the AppInit event, and can be configured in the event definition of the project. The system defaults The definition and configuration are as follows:
return [ 'bind' => [ ], 'listen' => [ 'AppInit' => [ 'think\listener\LoadLangPack', 'think\listener\RouteCheck', ], 'AppBegin' => [ 'think\listener\CheckRequestCache', ], 'ActionBegin' => [], 'AppEnd' => [], 'LogLevel' => [], 'LogWrite' => [], 'ResponseSend' => [], 'ResponseEnd' => [], ], 'subscribe' => [ ], ];
The think\listener\RouteCheck class will be executed in the AppInit event. If your application does not need to use any routing function at all, you can cancel the definition in the configuration file, and the system will Execute the default URL dispatch (i.e. controller/action).
Option and pattern parameters of the cancel registration method
Cancel the route registration method (including rule/get/post/put/delete/patch/miss/group and other methods) The option and pattern parameters are all changed to the method calling form. For example, the original:
Route::get('hello/:name', 'index/hello', [ 'ext' => 'html'], [ 'name' => '\w+']);
needs to be changed to
Route::get('hello/:name', 'index/hello') ->ext('html') ->pattern([ 'name' => '\w+']);
Routing group definition no longer supports arrays
Because it is not conducive to the nesting function of groups, routing group definitions no longer support arrays and can only be defined using closures. For example:
Route::group('blog', [ ':id' => 'Blog/read', ':name' => 'Blog/read', ])->ext('html')->pattern(['id' => '\d+']);
must be changed to
Route::group('blog', function() { Route::get(':id', 'Blog/read'); Route::get(':name', 'Blog/read'); })->ext('html')->pattern(['id' => '\d+']);
if you need To register a virtual routing group, you can directly use the closure in the first parameter
Route::group(function() { Route::get('blog/:id', 'Blog/read'); Route::get('user/:name', 'User/read'); })->ext('html')->pattern(['id' => '\d+']);
Cancel the url_controller_layer configuration
Instead use the controllerLayer method setting in the entry file .
(new App())->controllerLayer('Action') ->run() ->send();
Cancel the class_suffix configuration
Instead use the useClassSuffix method in the entry file.
(new App())->useClassSuffix(true) ->run() ->send();
Cancel the controller_suffix and class_suffix configuration parameters at the same time.
Cancel the mergeExtraVars method and corresponding parameters
Instead, explicitly specify the variable rules in the routing rules.
Header method parameter type adjustment
Due to strong type constraints, the header method is changed to only support the passing of array parameters.
Use strong type parameters
Since strong type parameters are fully enabled and strict mode is used, be sure to pay attention to the type of the parameters.
Many ThinkPHP introductory tutorials, all on the PHP Chinese website, welcome to learn online!
This article is reproduced from: https://blog.thinkphp.cn/916515
The above is the detailed content of ThinkPHP5.2: Routing adjustments and improvements. For more information, please follow other related articles on the PHP Chinese website!