请求对象
- 当控制器继承了控制器基类时,自动会被注入 Request 请求对象的功能;
class Rely extends Controller {public function index(){return $this->request->param('name');}}
- Request 请求对象拥有一个 param 方法,传入参数 name,可以得到相应的值;
3. 如果我们不继承控制器基类,可是自行注入 Request 对象,依赖注入后面会讲;use think\Request;class Rely {public function index(Request $request){return $request->param('name');}}
- 还可以通过构造方法进行注入,通过构造注入,就不需要每个方法都注入一遍;
use think\Request;class Rely{protected $request;public function __construct(Request $request){$this->request = $request;}public function index(){return $this->request->param('name');}}
- 使用 Facade 方式应用于没有进行依赖注入时使用 Request 对象的场合;
use think\facade\Request;class Rely {public function index(){return Request::param('name');}}
- 使用助手函数 request()方法也可以应用在没有依赖注入的场合;
class Rely {public function index(){return request()->param('name');}}
请求信息
- Request 对象除了 param 方法外,还有一些请求的固定信息,如表:

2. 上表的调用方法,直接调用即可,无须传入值,只有极个别如果传入 true 获取完 整 URL 的功能;Request::url();// 获取完整 URL 地址 包含域名Request::url(true);// 获取当前 URL(不含 QUERY_STRING) 不带域名Request::baseFile();// 获取当前 URL(不含 QUERY_STRING) 包含域名Request::baseFile(true);// 获取 URL 访问根地址 不带域名Request::root();// 获取 URL 访问根地址 包含域名Request::root(true);
