博客列表 >请求对象的依赖注入

请求对象的依赖注入

依然很m丶的博客
依然很m丶的博客原创
2018年08月26日 14:08:03802浏览

请求对象的依赖注入:

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    protected $request
  public function index()
  {
      return '正在学习中...';
  }
  public function demo1(Request $request)  //请求对象依赖注入
  {
      return $request->param('lesson');
  }
    public function demo2()               //没有注入到demo2方法
    {
      return $request->param('lesson');
    }
}

访问方式:

                     www.tp5.com/index/index/demo1/lesson/thinkphp5

返回:

                     thinkphp5

---------------------------------------------------------------------------------------------------------------------------------

构造方法:

<?php
namespace app\index\controller;
use think\Request;
class Index
{
    protected $request;                          //构造方法 可以被所有操作所共享
    public function __construct(Request $request)
    {
       $this->request= Request::instance();
    }
    public function index()
  {
      return '正在学习中...';
  }
  public function demo1() 
  {
      return $this->request->param('lesson');
  }
    public function demo2()               
    {
      return $this->request->param('lesson');
    }
}

访问方式:

                     www.tp5.com/index/index/demo1/lesson/thinkphp5

返回:

                     thinkphp5


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议