thinkphp is an open source PHP framework that is widely used in the field of Web development. In development, the jump page function is often used. This article will introduce how to use thinkphp to implement the jump page.
1. Use the redirect function to jump to the page
In thinkphp, you can use the redirect function to jump to other pages. The usage of this function is as follows:
public function redirect($url, $params = [], $code = 302, $withPrefix = false)
Among them, $url represents the page path to be jumped, $params represents the parameters that need to be passed when jumping, $code represents the HTTP status code of the jump, and $withPrefix represents whether Bring the domain name prefix. Here are some examples of using this function:
1. Jump to other controller methods
// 跳转到Home控制器的index方法 return $this->redirect('home/index');
2. Jump to external URL
return $this->redirect('http://www.example.com');
3. With parameters Jump
// 跳转到Home控制器的detail方法,并传递id参数 return $this->redirect('home/detail', ['id' => 1]);
2. Use the url function to generate a jump path
In addition to using the redirect function to jump to the page, you can also use the url function to generate a jump path, and then use the redirect function to jump. . The usage of the url function is as follows:
public function url($url = '', $vars = '', $suffix = true, $domain = false)
Among them, $url represents the URL address to be generated, $vars represents the parameters to be passed, $suffix represents whether to enable the URL suffix, and $domain represents whether to include the domain name prefix.
The following is an example of using the url function to generate a jump path:
// 生成Home控制器的index方法的URL $url = $this->url('home/index'); return $this->redirect($url);
The above are two methods of using thinkphp to jump to a page. Although these methods are simple, they can help us implement the jump page function more flexibly.
The above is the detailed content of How to implement jump page in thinkphp (two methods). For more information, please follow other related articles on the PHP Chinese website!