TP---跳转+传参总结

一、在controller和model中
1、
以下两个方法需要在控制器继承controller方法

use thinkController;
$this->success('操作成功!',url("index/index",['id'=>$id]));

设置成功后跳转页面的地址,默认的返回页面是$_SERVER[‘HTTP_REFERER’]。

$this->error('操作失败!',url("index/index",['id'=>$id]));

错误页面的默认跳转页面是返回前一页,通常不需要设置。
值得注意的是以上两个方法传递的参数get和post都接收不到,只能用input接收,或者是下面的方法:
首先调用Request函数方法

use thinkRequest;

然后使用Request函数接收值,$res 为接收到的所有值。

$request = Request::instance();
        $res=$request->param();

没有提示语直接跳转,用法和接收方法同上。
2、
重定向方法:

$this->redirect('index/index', ['id' => 2]);

二、在view中:
1、直接写

<a href="index?id=1">测试1</a>//只写方法名,只能在本控制器中跳转
<a href="index/index?id=1">测试1</a>//控制器名/方法名
<a href="index/index/index?id=1">测试1</a>//模块名/控制器名/方法名

2、用TP自带的方法

<a href="{:url('index')}?id=1">测试1</a>//只写方法名,只能在本控制器中跳转
<a href="{:url('index/index')}?id=1">测试1</a>//控制器名/方法名
<a href="{:url('index/index/index')}?id=1">测试1</a>//模块名/控制器名/方法名

以上两种方法可以用get接收值
3、类似于第二种
{:url(‘admin/index/index’,[‘id’=>1,‘name’=>‘admin’])}
接收方法:
只能用input接收,或者是下面的方法:
首先调用Request函数方法

use thinkRequest;

然后使用Request函数接收值,$res 为接收到的所有值。

$request = Request::instance();
$res=$request->param();
博客园:https://www.cnblogs.com/huixincode
如果此文章对您有所帮助记得打赏哦,一分也是对我的支持和鼓励,谢谢!
转载文章请务必保留出处和署名,谢谢!
原文地址:https://www.cnblogs.com/shx1024/p/11931241.html