请求对象的依赖注入:
<?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
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号