Home PHP Framework ThinkPHP ThinkPHP5.2: Routing adjustments and improvements

ThinkPHP5.2: Routing adjustments and improvements

Dec 16, 2019 pm 03:10 PM
thinkphp5.2 routing

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',
];
Copy after login

must be changed to:

Route::get('hello/:name', 'index/hello');
Copy after login

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应用的路由定义文件
Copy after login

The default URL rule becomes

http://域名/入口文件(或者应用名)/控制器名/操作名
Copy after login

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();
Copy after login

You can automatically access multiple applications through URLs without creating an entry file

http://serverName/index.php/admin
Copy after login

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();
Copy after login

Supports alias mapping of application names, for example:

(new App())->autoMulti([
    'think'    =>    'admin',  // 把admin应用映射为think
])->run()->send();
Copy after login

If you need to customize an application, you can use

(new App())->autoMulti([
    'admin'    =>   function($app) {
        $app->debug(true)->useClassSuffix();
    }
])->run()->send();
Copy after login

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' => [
    ],
];
Copy after login

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+']);
Copy after login

needs to be changed to

Route::get('hello/:name', 'index/hello')
    ->ext('html')
    ->pattern([ 'name'  => '\w+']);
Copy after login

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+']);
Copy after login

must be changed to

Route::group('blog', function() {
    Route::get(':id', 'Blog/read');
    Route::get(':name', 'Blog/read');
})->ext('html')->pattern(['id' => '\d+']);
Copy after login

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+']);
Copy after login

Cancel the url_controller_layer configuration

Instead use the controllerLayer method setting in the entry file .

(new App())->controllerLayer('Action')
    ->run()
    ->send();
Copy after login

Cancel the class_suffix configuration

Instead use the useClassSuffix method in the entry file.

(new App())->useClassSuffix(true)
    ->run()
    ->send();
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to Fix No Internet Security Issue on Windows 11/10 How to Fix No Internet Security Issue on Windows 11/10 May 11, 2023 pm 10:07 PM

One of the internet connection related issues seen on Windows 11/10 computers is the “No internet, secure” error message. Basically, this error message indicates that the system is connected to the network, but due to issues with the connection, you are unable to open any web pages and receive data. You may encounter this error while connecting to any network in Windows, preferably when connecting to the Internet through a WiFi router that is not nearby. Normally, when you check the wireless icon in the lower right corner of your system tray, you'll see a small yellow triangle, and when you click it, a No Internet, Security message will appear. There is no specific reason why this error message occurs, but changes to configuration settings may cause your router to be unable to connect

How to implement API routing in the Slim framework How to implement API routing in the Slim framework Aug 02, 2023 pm 05:13 PM

How to implement API routing in the Slim framework Slim is a lightweight PHP micro-framework that provides a simple and flexible way to build web applications. One of the main features is the implementation of API routing, allowing us to map different requests to corresponding handlers. This article will introduce how to implement API routing in the Slim framework and provide some code examples. First, we need to install the Slim framework. The latest version of Slim can be installed through Composer. Open a terminal and

3 Ways to Fix Router Lost Contact with NTP Time Server Error 3 Ways to Fix Router Lost Contact with NTP Time Server Error May 22, 2023 pm 03:43 PM

Connection and WiFi issues can be very frustrating and significantly reduce productivity. Computers use Network Time Protocol (NTP) for clock synchronization. In most cases, if not all, your laptop uses NTP to track time. If your server has lost contact due to NTP time server error message, read this article to the end to learn how to fix it. What happens when the router's time is set incorrectly? Router performance is generally not affected by incorrect time settings, so your connection may not be affected. However, some problems may arise. These include: Incorrect time for all gadgets that use the router as a local time server. The timestamps in the router log data will be wrong. if due to

Java Apache Camel: Building a flexible and efficient service-oriented architecture Java Apache Camel: Building a flexible and efficient service-oriented architecture Feb 19, 2024 pm 04:12 PM

Apache Camel is an Enterprise Service Bus (ESB)-based integration framework that can easily integrate disparate applications, services, and data sources to automate complex business processes. ApacheCamel uses route-based configuration to easily define and manage integration processes. Key features of ApacheCamel include: Flexibility: ApacheCamel can be easily integrated with a variety of applications, services, and data sources. It supports multiple protocols, including HTTP, JMS, SOAP, FTP, etc. Efficiency: ApacheCamel is very efficient, it can handle a large number of messages. It uses an asynchronous messaging mechanism, which improves performance. Expandable

How to Fix iPhone WiFi Keeps Disconnecting Repeatedly [Solved] How to Fix iPhone WiFi Keeps Disconnecting Repeatedly [Solved] May 20, 2023 pm 01:55 PM

Many iPhone users have expressed disappointment with one of the serious issues they face on their iPhone. The problem is that their iPhone disconnects from Wi-Fi every now and then. This is indeed a major issue since Wi-Fi is a necessity to use most apps on your iPhone. We have thoroughly analyzed this issue and identified the factors that may be responsible and listed them below. Auto-join settings are disabled Some issues in network settings Change Wi-Fi password Changed Wi-Fi router issues After looking into these factors mentioned above, we have compiled a set of solutions that can fix disconnection issues with Wi-Fi issues iPhone. Fix 1 – Turn on Wi-Fi’s auto-join setting if Wi-Fi is not enabled

How to use routing in ThinkPHP6 How to use routing in ThinkPHP6 Jun 20, 2023 pm 07:54 PM

ThinkPHP6 is a powerful PHP framework with convenient routing functions that can easily implement URL routing configuration; at the same time, ThinkPHP6 also supports a variety of routing modes, such as GET, POST, PUT, DELETE, etc. This article will introduce how to use ThinkPHP6 for routing configuration. 1. ThinkPHP6 routing mode GET method: The GET method is a method used to obtain data and is often used for page display. In ThinkPHP6, you can use the following

Can routing be used to defend against ARP spoofing, suppress broadcast storms, and defend against intranet viruses? Can routing be used to defend against ARP spoofing, suppress broadcast storms, and defend against intranet viruses? May 22, 2023 am 08:52 AM

The routes mentioned here are those above one thousand yuan, and we won’t talk about those below one thousand yuan. Nowadays, many enterprise routers say they have such a function, but such a function requires a prerequisite, that is, the computer must be directly connected to the router. If it is separated by a switch, these functions will be useless to the computer. Problems such as broadcast storms and ARP spoofing in the LAN are very common problems. They are not big problems, but they are very annoying. It is not difficult to solve broadcast storms, ARP spoofing or network loops. The difficulty lies in how to detect these problems. Recommend the "Buddha Nature" plug-in for our system. The reason why we say "Buddha nature" is because this detection function is based on Internet behavior management and the core of network monitoring data analysis, and only Internet behavior management can do it. It should have been placed in

4 Ways to Find or View WiFi Password on Windows 11 4 Ways to Find or View WiFi Password on Windows 11 Apr 23, 2023 pm 01:19 PM

Find WiFi password on Windows 11: Is it easy? Yes, you can easily view your saved WiFi passwords in Windows 11 using any of the methods mentioned below. You need administrator rights to view saved WiFi passwords on a specific device. Additionally, in some cases, devices paired with the router using WPS may not display the decrypted password. How to View Your WiFi Password on Windows 11 in 4 Easy Ways Here’s how to view your saved WiFi password in Windows 11. Follow any of the methods below based on your preferences and requirements. Method One: Use Control Panel to View WiFi Password

See all articles