1 tp5新增了Request和Response两个对象,分别用于接收和返回数据,要使用这些对象,请先用use声明(打开)命名空间
2 获取$_GET变量
namespace app\index\controller
use think\Request;
class Index extends Controller{
//自动实例化对象,可使用tp写的_initialize()方法//注意这是一个_
public function __construct(Request $request = null)
{
$this->token = $request->header('token');
if ($this->token == null){
$this->token = false;
}
parent::__construct($request);
}
public function index(Request $request = nul) //注入请求对象
{
我们假设你的url是这样的
http://xxxx/home/Index/index/id/123/a/456/b/789
$a=$request ->get('a')
/*
这时候你打印数据,发现a是空的,这是因为tp5的获取方式不同,就是说它不支持获取pathinfo格式的数据,要获取这些get数据,请使用tp提供的param方法,这个是可以自动检测当前的请求类型,并取得数据,可以获取的数据类型包括GET POST PUT DELETE等.详情请查看源码
tp5中正确的获取方式
*/
$a=$request ->param('a')
}
}
可能你不了解PUT DELETE这些请求,这个没事.我会在之后的文章中介绍给大家,也会对tp的源码进行拆解分析,欢迎大家的关注
初次写技术博客,如果不好,请见谅
网友评论