从控制器中调用Ruquest类的四种方法:
* 1.传统的new Request
* 2.静态代理:think\facade\Request
* 3.依赖注入:Request $request
* 4.父类Controller中的属性$request :$this->request
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/1/21 0021 * Time: 23:42 */ namespace app\index\controller; use think\Request; use think\Controller; class Tast extends Controller { //方法一:传统的new Request方法 public function test1() { $request = new Request(); dump($request->get()); } }
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/1/21 0021 * Time: 23:42 */ namespace app\index\controller; use think\facade\Request;//导入请求对象的静态代理 class Tast { public function test2() { //用静态代理:think\facade\Request dump( Request::get()); } }
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/1/21 0021 * Time: 23:42 */ namespace app\index\controller; use think\Request; class Tast { //用依赖注入 public function test3(Request $request) { dump($request->get()); } }
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/1/21 0021 * Time: 23:42 */ namespace app\index\controller; use think\Controller;//导入think命名空间的Controller类 use think\Request;//导入think命名空间的Request类 class Tast extends Controller { //直接调用父类Controller中的属性:$request : $this->request public function test4() { dump($this->request->get()); } }
运行效果图: