Blogger Information
Blog 94
fans 0
comment 0
visits 92685
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类调用的4种方法(Facade)
可乐随笔
Original
1266 people have browsed it

类调用的4种方法(Facade)

1.传统的new Request
2.静态代理:think\facade\Request;
3.依赖注入:Request $request;
4.父类Controller中的属性$request : this->request->get();

注:

  • 正常情况下,控制器不依赖父类Controller.php
  • 推荐继承父类,可以很方便的使用父类中的封装好的一些方法和属性
  • Controller.php 没有静态代理
  • 控制器中的输出,字符串全部用return返回,不要用echo
  • 如果输出的是复杂类型,我们可以用dump()函数
  • 默认输出的格式为html,可以指定为其他格式:json
  1. <?php
  2. //导入请求对象的静态代理
  3. //use think\Facade\Request;
  4. //引用动态类
  5. use think\Request;
  6. namespace app\index\controller;
  7. use think\Container;
  8. use function MongoDB\BSON\toJSON;
  9. class Demo3 extends Container
  10. {
  11. public function test()
  12. {
  13. // dump(Request::get());
  14. // $request = new Request();
  15. // dump($this->request->get());
  16. //以Json方式输出
  17. return json_encode($this->request->get());
  18. // return 'hello php.cn ';
  19. }
  20. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post