Home> PHP Framework> ThinkPHP> body text

How to use ThinkPhp routing

PHPz
Release: 2023-04-11 14:59:38
Original
1207 people have browsed it

ThinkPhp is a commonly used PHP framework, and routing is an important part of building web applications. This article will introduce the basic concepts of ThinkPhp routing, how to use it, and some practical tips to help readers better understand and apply ThinkPhp routing.

1. Basic concepts of ThinkPhp routing

Routing is the process of mapping HTTP requests to handlers. In the traditional MVC architecture, routing plays a vital role. We can understand routing as a mapping between an address and an application. The address requested by the client will be routed to the corresponding controller method, and then the corresponding content will be output.

ThinkPhp routing mainly consists of three parts: controller (Controller), operation method (Action) and parameter (Param).

1. Controller: The controller is the first layer that specifies request routing and represents a controller class. The role of the controller is to receive Web requests, process the requests and return the third parameter, Action (operation method).

2. Operation method (Action): Each controller contains an operation method. After the controller receives the request, it hands the request to the operation method for processing and returns the result.

3. Parameter (Param): Parameters are flags used to distinguish different behaviors of operation methods within the same controller. For example, we can use parameters to distinguish between two different operations: querying users with a certain ID number and querying all user information.

In ThinkPhp, routes are defined through URL access. The URL contains the information of the above three parts.

For example:

http://www.example.com/index.php/index.php/controller/action/param1/value1/param2/value2

Among them,index.phprepresents a single entry file,controllerrepresents the controller,actionrepresents the operation method,paramIndicates parameters,valueindicates the value of the corresponding parameter.

2. How to use ThinkPhp routing

The following introduces how to use ThinkPhp routing:

1.Basic routing

All routes in the application will be mapped by default to a specific controller and method. For example, the request URL is/index.php/index/index, which will be mapped to the index method of the Index class.

2. Static routing

Static routing refers to a type of route that does not contain parameters in the URL. This type of route can use some kind of regular expression to match URLs. Static routes map URLs to specified controllers and methods and do not change during execution.

For example:

use think\facade\Route; Route::get('blog/:id','index/blog/read');
Copy after login

The above code defines a static route, which will route to the read method of Index's blog controller. In fact, it will match the request as/index.php The URL of /blog/4, where 4 is the value of the parameter id.

3.Dynamic routing

Dynamic routing refers to a route that contains parameters. For example, if we want to get the current user's profile page, we can use the following route:

use think\facade\Route; Route::rule('user/:id', 'index/user/index');
Copy after login

This route defines the parameters that contain the user id in the URL, for example:/index.php/user/ 123, where 123 will be passed as the value of parameter id to the index method of the user controller.

4. Regular routing

Regular routing is a route that matches the request URL according to rules. It can check and match part of the request URL, and map it to specific controllers and action methods based on the matching results defined by the rules.

For example, the following route will match requests that contain numbers in the URL, such as/index.php/test/123. If you replace the numbers in the URL with letters, there will be no match.

use think\facade\Route; Route::rule('^test/(\d+)$', 'Test/index/:1');
Copy after login

The above route will match URLs that contain a number in the request URL and use the number as the first parameter of the controller. For example, the request URL is/index.php/test/123, which will be mapped to the index method of the Test controller and use 123 as the first parameter.

5. Group routing

Packet routing refers to dividing the route into several parts and defining the routes of different controllers in each part. Using group routing simplifies routing configuration while increasing code readability.

For example, group routing can also be used to define unlimited routes:

use think\facade\Route; Route::group('', function () { Route::rule('index', 'index/index/index'); Route::rule('user', 'index/user/index'); Route::rule('company', 'index/company/index'); });
Copy after login

The above defines 3 routes, which are mapped to different controllers.

3. Practical tips for ThinkPhp routing

1. Custom routing rules

You can define your own URL routing rules through Route::rule(). For example, use the following code to override the default controller and operation method:

use think\facade\Route; Route::rule('admin/:controller/:action', 'admin/:controller/:action')->middleware('CheckLogin');
Copy after login

The above code defines the controller for background employee access, and the actions of each controller need to be verified by the CheckLogin middleware.

2. Dynamically generate routes

When you have multiple routes that need to be routed to the same controller or operation method, you can define them through dynamic routing. This eliminates the need to define separate request parameters for each case-sensitive route.

For example, you can use the following code to dynamically generate routes:

use think\facade\Route; $router = Route::group('home', function(){ Route::rule('news/:id','index/news/detail')->pattern(['id' => '\d+']); Route::rule('jobs/:year/:month/:day/:id', 'index/jobs/detail') ->pattern(['year' => '\d+', 'month' => '\d+', 'day' => '\d+', 'id' => '\d+']); Route::rule('products','index/products/index'); });
Copy after login

The above code points the routes of different delivery methods to a certain operation method in the index controller, and uses regular expressions to match dynamic parameters.

3. Route distribution

If there are multiple different sub-applications in your application, you can use distribution routing to distribute requests to different sub-applications.

For example, you can use the following code to implement route distribution:

use think\facade\Route; Route::group('app', function () { Route::rule('blog/:year/:month/:day/:name', 'blog/detail'); Route::rule('profile', 'user/profile'); Route::rule('account/login', 'user/login'); }); Route::miss('home/index/missed');
Copy after login

其中,miss方法用来定义路由未匹配时执行的逻辑。

四、结语

本文介绍了ThinkPhp路由的基本概念、使用方法以及一些实用技巧。掌握了路由的概念和使用方法,可以让开发者更加高效地开发Web应用程序。

The above is the detailed content of How to use ThinkPhp routing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!