Home > PHP Framework > ThinkPHP > How to perform controller jump in ThinkPHP framework

How to perform controller jump in ThinkPHP framework

WBOY
Release: 2023-05-30 13:19:32
forward
1249 people have browsed it

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

Use the redirect method of the Controller object to realize the page jump in ThinkPHP. This method allows the use of two parameters: the first parameter is used to determine the URL address of the jump, and the second parameter specifies 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. Define routing rules in the configuration file, 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 implements 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 you 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(&#39;home/index/index&#39;,[&#39;id&#39;=>1]);
    $this->success(&#39;跳转成功&#39;, $url);
}
Copy after login

Jump to the index method of the Home controller and pass the id parameter value as 1. After one second, the page will automatically jump to the predetermined URL and a "Jump successful" prompt will be displayed.

The above is the detailed content of How to perform controller jump in ThinkPHP framework. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template