Home  >  Article  >  PHP Framework  >  How thinkphp gets the parameters of url

How thinkphp gets the parameters of url

PHPz
PHPzOriginal
2023-04-17 09:48:582262browse

ThinkPHP is an open source PHP application framework, which often needs to obtain URL parameters during development. Today we will discuss how ThinkPHP obtains URL parameters.

In ThinkPHP, there are many ways to obtain URL parameters. Below we will introduce three of the more commonly used methods.

1. Use the input function to obtain the parameters of the URL

In the controller, you can use the input function to obtain the parameters of the URL. This function can obtain the parameters submitted by GET and POST.

Next, we take the controller's need to obtain the id parameter as an example:

$id = input('get.id');

In the above code, we use the input function to obtain the id parameter submitted in the get method.

If you need to get the parameters submitted in post mode, just change 'get.' to 'post.'.

2. Use the helper function input to obtain the parameters of the URL

ThinkPHP provides a series of helper functions that allow us to operate ThinkPHP more conveniently.

Among them, input is a very commonly used helper function. It can easily obtain parameters submitted by GET and POST.

Also taking the controller needing to obtain the id parameter as an example, the following is the code for using the input helper function to obtain the id parameter:

$id = input('get.id');

The method is the same as using the input function to obtain the URL parameter, except that Just a helper function.

3. Use the helper function request to obtain URL parameters

In addition to the input function, ThinkPHP also provides another helper function request to obtain URL parameters.

The request function can easily obtain parameters submitted by GET, POST, PUT, DELETE, etc. Here we only introduce getting the parameters submitted by GET method.

Similarly, the controller needs to obtain the id parameter as an example. The following is the code for using the request helper function to obtain the id parameter:

$id = request()->param('id');

Different from the first two methods, the request function needs to use the param method. Get parameters. However, the advantage of using the request function is that you can easily obtain parameters submitted by PUT, DELETE, etc.

Summary

Among the above three methods, using the input function and input assistant function is the most commonly used method. It is very convenient to use these two methods to obtain URL parameters.

The advantage of using the request function is that you can easily obtain parameters submitted by PUT and DELETE, but the usage rate in our daily development is still not very high.

In short, when developing using the ThinkPHP framework, mastering the above methods of obtaining URL parameters can allow us to develop efficient applications faster.

The above is the detailed content of How thinkphp gets the parameters of url. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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