Tp5获取url请求对象的依赖注入
2019-03-13 本文已影响0人
ZSGZ_AD
方案一:
use think\Request;
class Index
{
protected $request;
public function __construct()
{
$this->request = Request::instance();
}
//第一种方式构造初始化
public function demo1()
{
return '学习的课程:' . $this->request->param('lesson');
}
//第二种方式
public function demo2()
{
return '学习的课程:' . Request::instance()->param('lesson');
}
}
方案二: 继承父类
class Index extends \think\Controller
{
public function demo1(){
return $this->request->siteName;
}
public function demo2(){
return $this->request->getSiteName();
}
}