Home > PHP Framework > ThinkPHP > How to pass variables in thinkphp controller

How to pass variables in thinkphp controller

PHPz
Release: 2023-04-14 11:17:45
Original
997 people have browsed it

thinkphp is a very popular PHP development framework. It has the advantages of simplicity, ease of use, powerful functions, and excellent performance. It has been widely used in the development of many Web applications. In the thinkphp framework, the controller is the core part responsible for processing user requests. It implements data extraction and processing through the controller, and finally returns the results to the user. In this process, how the controller transfers and processes variables is a very critical issue. This article will delve into how to transfer variables in the thinkphp controller and how to ensure the correctness and efficiency of the transfer process.

1. Variable transfer method

  1. GET method

The controller can obtain the parameters passed by the client through the GET method. Pass parameters in GET mode and put the parameters in the URL, such as http://www.example.com/index.php?Parameter name=Parameter value. Use the GET method to pass parameters. The parameters will be exposed in the URL, so it is not suitable for passing sensitive data. You need to be careful when using it.

To obtain the parameters passed by GET method, you can use the input method provided by the thinkphp framework to receive the parameters passed by get method in the URL address. For example:

$id = input('id')
Copy after login
  1. POST method

Use POST method to pass parameters. The parameters will not appear in the URL, so it is suitable for transmitting sensitive data, such as the user's account password, bank Card number, etc. Use the POST method to pass parameters, and you can also use the input method to receive them in the controller. For example:

$name = input('post.name');
Copy after login
  1. Routing method

The routing method is more flexible in passing parameters. You can customize the URL address and put the parameters in the URL. In the routing configuration of the thinkphp framework, :id can be passed to the controller as a parameter in the form of 'router' => ['/:id' => 'index/hello']. For example:

Routing configuration:

'router' => [
   '/user/:id' => 'User/index'
],
Copy after login

Obtaining parameters in the controller:

public function index($id)
{
    echo 'User ID:' . $id;
}
Copy after login

2. How to avoid the error of not passing variables?

  1. Add default values ​​to parameters

In the controller of the thinkphp framework, we can set a default value for each parameter that receives a variable to ensure that even if it is not passed There will be no errors with the parameters. For example:

public function test($id='',$name='',$age='')
{
    echo $id,$name,$age;
}
Copy after login
  1. Determine whether the variable exists

In the controller, we can use the isset() function to determine whether the parameter is passed:

if(isset($_REQUEST['id']))
{
  $id=$_REQUEST['id'];
}
else
{
  $id=0;
}
Copy after login

But in the thinkphp framework, we recommend using the has() method of the request class to determine whether parameters are passed. For example:

if(request()->has('id'))
{
  $id=request()->param('id');
}
else
{
  $id=0;
}
Copy after login

3. Think about the efficiency of controller variable transfer?

The transfer of variables will also affect the operating efficiency of the controller. Passing variables requires memory space overhead, and the operating efficiency of the controller directly affects the response speed of the entire system. Therefore, we need to make passing variables as efficient as possible.

  1. Use static variables

You can use static variables to store frequently used variables to reduce the time spent on passing the same variables. For example:

class OrderController extends Controller
{
    protected static $userId;

    public function initialize()
    {
        self::$userId = input('userId');
    }

    public function index()
    {
        // 利用self::$userId使用静态变量
    }
}
Copy after login
  1. Use global variables

You can use global variables among variables that you want to use frequently, and different controllers can use them. Using global variables does not require variable parameter passing and can also reduce memory overhead. For example:

$GLOBALS['userId'] = input('userId');

class OrderController extends Controller
{
    public function index()
    {
        echo $GLOBALS['userId'];
    }
}

class UserController extends Controller
{
    public function index()
    {
        echo $GLOBALS['userId'];
    }
}
Copy after login
  1. Use parameter binding

Through parameter binding, the parameters are directly bound to the function parameters, which reduces the process of variable transfer and improves the program operating efficiency. For example:

class OrderController extends Controller
{
    public function index($userId)
    {
        // 直接使用 $userId,避免了变量传递
    }
}
Copy after login

In general, passing variables correctly and efficiently in the thinkphp framework can speed up the running speed of the program and reduce resource usage. Using the above delivery methods and techniques, we can better improve code quality and develop better web applications.

The above is the detailed content of How to pass variables in thinkphp controller. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template