Home > Article > PHP Framework > Two methods for page jump in ThinkPHP5
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.
The jump assistant function realizes page jump through redirect()
. redirect()
The function accepts one parameter, which is the jump address.
public function index() { // 跳转到Index控制器中的hello方法 return redirect('index/hello'); } public function hello() { return 'Hello, ThinkPHP5!'; }
public function index() { // 跳转到http://www.example.com/ return redirect('http://www.example.com/'); }
public function index() { // 跳转到Index控制器中的hello方法,并传递参数name return redirect('index/hello', ['name' => 'ThinkPHP5']); } public function hello($name) { return 'Hello, ' . $name . '!'; }
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.
use think\Controller; class Index extends Controller { public function index() { // 跳转到Index控制器中的hello方法 return $this->redirect('hello'); } public function hello() { return 'Hello, ThinkPHP5!'; } }
use think\Controller; class Index extends Controller { public function index() { // 跳转到http://www.example.com/ return $this->redirect('http://www.example.com/'); } }
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!