在thinkphp5中,跳转地址是一个非常常见的需求。本文将介绍如何在thinkphp5中进行页面跳转。
在ThinkPHP5中,有两种方式可以实现页面跳转。
方式一:使用跳转助手函数
跳转助手函数通过 redirect() 实现页面跳转。redirect() 函数接受一个参数,即跳转地址。
1. 跳转到控制器中的方法
public function index()
{
// 跳转到Index控制器中的hello方法
return redirect('index/hello');
}
public function hello()
{
return 'Hello, ThinkPHP5!';
}2. 跳转到URL地址
public function index()
{
// 跳转到http://www.example.com/
return redirect('http://www.example.com/');
}3. 带参数跳转
public function index()
{
// 跳转到Index控制器中的hello方法,并传递参数name
return redirect('index/hello', ['name' => 'ThinkPHP5']);
}
public function hello($name)
{
return 'Hello, ' . $name . '!';
}方式二:使用控制器基类的 redirect 方法
ThinkPHP5中的控制器基类(Controller)中提供了 redirect() 方法来实现页面跳转。这种方式比使用跳转助手函数更加灵活。
采用 php+mysql 数据库方式运行的强大网上商店系统,执行效率高速度快,支持多语言,模板和代码分离,轻松创建属于自己的个性化用户界面 v3.5更新: 1).进一步静态化了活动商品. 2).提供了一些重要UFT-8转换文件 3).修复了除了网银在线支付其它支付显示错误的问题. 4).修改了LOGO广告管理,增加LOGO链接后主页LOGO路径错误的问题 5).修改了公告无法发布的问题,可能是打压
1. 跳转到控制器中的方法
use think\Controller;
class Index extends Controller
{
public function index()
{
// 跳转到Index控制器中的hello方法
return $this->redirect('hello');
}
public function hello()
{
return 'Hello, ThinkPHP5!';
}
}2. 跳转到URL地址
use think\Controller;
class Index extends Controller
{
public function index()
{
// 跳转到http://www.example.com/
return $this->redirect('http://www.example.com/');
}
}3. 带参数跳转
use think\Controller;
class Index extends Controller
{
public function index()
{
// 跳转到Index控制器中的hello方法,并传递参数name
return $this->redirect('hello', ['name' => 'ThinkPHP5']);
}
public function hello($name)
{
return 'Hello, ' . $name . '!';
}
}以上就是在ThinkPHP5中实现页面跳转的方法,建议根据实际情况选择适合的方式进行跳转。
立即学习“PHP免费学习笔记(深入)”;










