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

How to perform controller jump in ThinkPHP framework

WBOY
WBOYforward
2023-05-30 13:19:321213browse

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');
}
  1. Define routing rules in the configuration file, for example:

// 路由定义
return [
    // 跳转
    'hello' => 'index/hello',
];

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]);
}

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('模块/控制器/操作',['参数']);

Use the page jump method to jump:

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

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();
}

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>
  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);
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete