Home > PHP Framework > ThinkPHP > body text

thinkphp modify jump

WBOY
Release: 2023-05-26 09:56:07
Original
411 people have browsed it

When using the ThinkPHP framework, we often need to perform jump operations. By default, ThinkPHP's jump function jumps to the specified URL address, for example:

$this->redirect('http://www.example.com');
Copy after login
Copy after login

But sometimes, we need to perform some additional operations or modify the default jump method when jumping. This article will introduce how to modify the ThinkPHP jump method.

Modify the jump method

In ThinkPHP, there are two ways of jump function: redirect and success/error.

redirect function

The redirect function is the default jump function provided by the ThinkPHP framework, which is used to jump to the specified URL address.

$this->redirect('http://www.example.com');
Copy after login
Copy after login

The first parameter of the redirect function is the URL address to be jumped. If you want to jump to a method of the current controller, you can use the U function to generate the URL address:

$this->redirect(U('index'));
Copy after login

The jump delay time can be set through the second parameter:

$this->redirect(U('index'), 3, '跳转中...');
Copy after login

The third parameter of the redirect function is the prompt information. If this parameter is set, the prompt information will be output before the jump.

The redirect function is a function in the thinkController class. In other classes, you can use $this->redirect() to jump.

success and error functions

The success and error functions are used to jump to the success and error pages that come with the framework. Their usage is as follows:

$this->success('操作成功', U('index'));
$this->error('操作失败', U('index'));
Copy after login

The first parameter is the prompt information, and the second parameter is the jump address. If the jump address is not set, it will jump to the last accessed address by default.

Modify the jump method

By default, the ThinkPHP framework jump function uses the Header function to jump, that is, jump directly to the specified URL address. But in some cases, we need to change the jumping method, for example, we need to perform certain operations before jumping, or need to pass some parameters when jumping, etc.

ThinkPHP framework provides a redirect method for modifying the jump method. Before jumping, perform certain operations and pass parameters before jumping.

$this->redirect('jump', array('id'=>1), 2, '跳转中...');
Copy after login

The above code will call the jump method:

public function jump($id){
    // do something
    $this->redirect('http://www.example.com');
}
Copy after login

We can perform some operations in the jump method, such as querying the database, setting up Session, etc. Then perform the jump operation again when jumping.

The first parameter of the redirect method is the method to be jumped, the second parameter is the parameter to be passed, the third parameter is the jump time, and the fourth parameter is the prompt information when jumping. .

Summary

In the ThinkPHP framework, the jump function is very commonly used and very flexible. By modifying the jump method, we can perform some operations before the jump to make the jump more intelligent. The above is my summary of ThinkPHP modification jumps.

The above is the detailed content of thinkphp modify jump. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!