tp5 怎么跳转php页面,TP5的页面跳转与重定向.md

本文介绍了ThinkPHP5中页面跳转的目标,包括使用`$this->success()`和`$this->error()`进行跳转,并展示了如何通过`Url::build()`和助手函数`url()`构建跳转地址。同时,讲解了设置URL访问重定向的方法,如使用`$this->redirect()`函数进行临时和永久重定向,并给出了具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、页面跳转的目标有哪些?

调用方法:

$this->success('提示','地址');

$this->error('提示','地址');

index.php文件内容:

namespace app\index\controller;

class Index extends \think\Controller

{

public function index()

{

return '

欢迎来到PHP中文网学习ThinkPHP5开发技术

';

}

public function hello($name)

{

if($name == 'thinkphp')

{

$this->success('验证成功,正在跳转~~~','ok');

} else {

$this->error('验证失败,正在返回登陆界面……','login');

}

}

public function ok()

{

return '欢迎使用后台管理系统';

}

public function login()

{

return '登陆页面';

}

}

?>

跳转地址这样写也是可以的:

$this->success('验证成功,正在跳转~~~',\think\Url::build('demo/login/ok'));

$this->success('验证成功,正在跳转~~~',\think\Url::build('https://www.bilibili.com/'));

$this->success('验证成功,正在跳转~~~',url('https://www.bilibili.com/'); //url是助手函数,功能同上

2、如何设置URL访问的重定向?

调用方法:

$this->redirect('路由地址',[变量列表],'后缀','域名开关');

index.php文件内容:

namespace app\index\controller;

class Index extends \think\Controller

{

public function index()

{

return '

欢迎来到PHP中文网学习ThinkPHP5开发技术

';

}

public function hello($name)

{

if($name == 'thinkphp')

{

/*******************以下是变化*******************/

$this->redirect('ok',['siteName'=>'php中文网']);

} else {

$this->redirect('http://www.php.cn',302); //302是临时重定向,301是永久重定向

}

}

public function ok($siteName)

{

return '欢迎来到'.$siteName.'学习ThinkPHP5开发技术';

}

public function login()

{

return '登陆页面';

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值