Blogger Information
Blog 14
fans 0
comment 0
visits 16097
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
请求对象的依赖注入
依然很m丶的博客
Original
813 people have browsed it

请求对象的依赖注入:

<?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


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