Home > PHP Framework > ThinkPHP > body text

How to make controller jump in ThinkPHP framework

PHPz
Release: 2023-04-07 13:59:31
Original
854 people have browsed it

ThinkPHP is an excellent PHP development framework. Based on the MVC development model, it provides complete routing management functions, allowing us to map different URL requests to different controllers and operation methods by configuring routing rules, thereby achieving flexibility Front and rear end separation design.

This article will introduce how to perform controller jumps in the ThinkPHP framework to realize page jumps and the function of passing parameters.

1. Use the redirect method of the Controller object to achieve jump

In ThinkPHP, you can achieve the jump through the redirect method of the Controller object. This method can accept two parameters, the first parameter represents the URL address of the jump, and the second parameter represents the parameter information that needs to be passed when jumping.

The specific implementation steps are as follows:

  1. Call the redirect method in the controller method, for example:
public function index()
{
    // 跳转到hello方法
    $this->redirect('hello');
}
Copy after login
  1. Defined in the configuration file Routing rules, for example:
// 路由定义
return [
    // 跳转
    'hello' => 'index/hello',
];
Copy after login

Here hello is mapped to the hello method of the Index controller.

  1. Implement the jump in the hello method of the controller, for example:
public function hello()
{
    // 跳转到/home/index/index方法
    $this->redirect('/home/index/index',['id'=>1]);
}
Copy after login

Here will jump to the index method of the Home controller and pass the id parameter as 1 .

2. Use the url function and page jump method to implement the jump

In addition to using the redirect method of the Controller object to implement the jump, you can also use the url function and page jump method to implement the jump. .

Use the url function to jump:

url('模块/控制器/操作',['参数']);
Copy after login

Use the page jump method to jump:

$this->success('提示信息', '跳转url');
Copy after login

The success method can accept three parameters, namely prompt information, Jump URL and waiting time, the default waiting time is 1 second.

The following are the specific implementation steps of using the url function and the page jump method to implement the jump:

  1. Use the url function to implement the jump, for example:
public function index()
{
    // 跳转到Home控制器的index方法
    $url = url('home/index/index',['id'=>1]);
    $this->assign('url', $url);
    return $this->fetch();
}
Copy after login

Here we will jump to the index method of the Home controller and pass the id parameter as 1.

Use a tag in the page to jump:

<a href="{$url}">跳转</a>
Copy after login
  1. Use the page jump method to jump, for example:
public function index()
{
    // 跳转到Home控制器的index方法
    $url = url('home/index/index',['id'=>1]);
    $this->success('跳转成功', $url);
}
Copy after login

Here will jump Go to the index method of the Home controller and pass the id parameter as 1. The page will display the prompt message "Jump successful" and automatically jump to the specified URL after 1 second.

3. Summary

Through the introduction of this article, we can see that it is not complicated to implement controller jump in the ThinkPHP framework. You can use the redirect method, url function and page jump of the Controller object. The transfer method implements the jump function. At the same time, we also introduced how to pass parameters to help developers design and develop applications more flexibly.

The above is the detailed content of How to make controller jump in ThinkPHP framework. 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