Home> PHP Framework> ThinkPHP> body text

How to use the immediate jump function in thinkphp3.2

WBOY
Release: 2023-05-30 22:56:06
forward
950 people have browsed it

In the ThinkPHP framework, the jump function can be implemented using the redirect method. Because this method is defined in the controller base class, the controller can call it directly to implement the immediate jump function. The redirect method accepts two parameters: $url and $params.

Among them, $url is the redirected URL address. The parameter can be a simple string, an array, or an anchor value starting with #. If the $Url parameter is an array, it can contain routing rule aliases, controller names, operation names, and parameter lists. That is, arrays can achieve different jump effects by specifying different elements.

$params is an optional parameter that can be used to specify parameters to append to the GET request. This parameter is only valid if there is no query string (i.e. "?") in the URL. If the URL already contains a query string, $params will be ignored.

The following is an example of using the redirect method:

class IndexController extends Controller { public function index() { // 直接跳转到百度首页 $this->redirect('http://www.baidu.com'); } public function test() { // 跳转到当前控制器Index操作中 $this->redirect('Index/index'); // 跳转到Home控制器中的Index操作 $this->redirect('Home/Index/index'); // 跳转并传递$name参数 $this->redirect('Index/index', array('name' => 'ThinkPHP')); // 跳转并传递多个参数 $this->redirect('Index/index', array('name' => 'ThinkPHP', 'id' => 100)); } }
Copy after login

In the above code example, the redirect method is used to implement various jump functions. For example,$this->redirect('http://www.baidu.com');is an operation that jumps directly to the Baidu homepage.$this->redirect('Index/index');jumps to the current controller Index operation.$this->redirect('Home/Index/index');refers to the Index operation in the Home controller.$this->redirect('Index/index', array('name' => 'ThinkPHP'));jumps and passes the $name parameter.$this->redirect('Index/index', array('name' => 'ThinkPHP', 'id' => 100));is to jump and pass multiple parameters.

In addition to redirecting the web page to the specified URL, the redirect method can also redirect it to the alias of the routing rule. In this case, the $url parameter should be a routing alias, not a URL address. For example, suppose we have the following routing rule:

// 路由规则定义 'link/:id' => 'Links/link',
Copy after login

We can use the routing rule alias "link" to call the redirect method in the controller to jump to the link page. For example:

class LinksController extends Controller { public function link() { $id = I('get.id'); $link = M('Links')->where(array('id'=>$id))->find(); // 验证链接是否存在 if ($link) { // 跳转到重定义的路由规则上 $this->redirect('link/'.$id); } else { $this->error('链接不存在!'); } } }
Copy after login

In the above code example, we used a redirect to the redefined routing rule alias in the link method of the Links controller."$this->redirect('link/'.$id);"A routing alias is used and parameters are passed to jump to the link page.

The above is the detailed content of How to use the immediate jump function in thinkphp3.2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!