Home  >  Article  >  PHP Framework  >  Two methods for page jump in ThinkPHP5

Two methods for page jump in ThinkPHP5

PHPz
PHPzOriginal
2023-04-11 10:30:052677browse

In ThinkPHP5, jump address is a very common requirement. This article will introduce how to jump to pages in ThinkPHP5.

In ThinkPHP5, there are two ways to achieve page jump.

Method 1: Use the jump assistant function

The jump assistant function realizes page jump through redirect(). redirect() The function accepts one parameter, which is the jump address.

1. Jump to the method in the controller

public function index()
{
    // 跳转到Index控制器中的hello方法
    return redirect('index/hello');
}

public function hello()
{
    return 'Hello, ThinkPHP5!';
}

2. Jump to the URL address

public function index()
{
    // 跳转到http://www.example.com/
    return redirect('http://www.example.com/');
}

3. Jump with parameters

public function index()
{
    // 跳转到Index控制器中的hello方法,并传递参数name
    return redirect('index/hello', ['name' => 'ThinkPHP5']);
}

public function hello($name)
{
    return 'Hello, ' . $name . '!';
}

Method 2: Use the redirect method of the controller base class

The controller base class (Controller) in ThinkPHP5 provides the redirect() method to implement page jump. This method is more flexible than using jump helper functions.

1. Jump to the method in the controller

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        // 跳转到Index控制器中的hello方法
        return $this->redirect('hello');
    }

    public function hello()
    {
        return 'Hello, ThinkPHP5!';
    }
}

2. Jump to the URL address

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        // 跳转到http://www.example.com/
        return $this->redirect('http://www.example.com/');
    }
}

3. Jump with parameters

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        // 跳转到Index控制器中的hello方法,并传递参数name
        return $this->redirect('hello', ['name' => 'ThinkPHP5']);
    }

    public function hello($name)
    {
        return 'Hello, ' . $name . '!';
    }
}

The above is the method to implement page jump in ThinkPHP5. It is recommended to choose the appropriate method to jump according to the actual situation.

The above is the detailed content of Two methods for page jump in ThinkPHP5. 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