多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
> ## 获得请求对象 ``` // 方法一:实例化对象 1.use think\Request; 2.$request = Request::instance(); // 方法二: 助手函数 $request = request(); // 方法三:依赖注入 1.use think\Request; 2.在方法的形参表添加Request $request ``` >## 获取URL信息 ``` $request = Request::instance(); // 获取当前域名 echo 'domain: ' . $request->domain() . '<br/>'; // http://tp5.com // 获取当前入口文件 echo 'file: ' . $request->baseFile() . '<br/>'; // /index.php // 获取当前URL地址 不含域名 echo 'url: ' . $request->url() . '<br/>'; // /index/index/hello.html?name=thinkphp // 获取包含域名的完整URL地址 echo 'url with domain: ' . $request->url(true) . '<br/>'; // http://tp5.com/index/index/hello.html?name=thinkphp // 获取当前URL地址 不含QUERY_STRING echo 'url without query: ' . $request->baseUrl() . '<br/>'; // /index/index/hello.html // 获取URL访问的ROOT地址 echo 'root:' . $request->root() . '<br/>'; // // 获取URL访问的ROOT地址 echo 'root with domain: ' . $request->root(true) . '<br/>'; // http://tp5.com // 获取URL地址中的PATH_INFO信息 echo 'pathinfo: ' . $request->pathinfo() . '<br/>'; // index/index/hello.html // 获取URL地址中的PATH_INFO信息 不含后缀 echo 'pathinfo: ' . $request->path() . '<br/>'; // index/index/hello // 获取URL地址中的后缀信息 echo 'ext: ' . $request->ext() . '<br/>'; // html ``` >## 获取或设置 模块/控制器/操作 ``` // 获取 $request = Request::instance(); echo "当前模块名称是" . $request->module(); echo "当前控制器名称是" . $request->controller(); echo "当前操作名称是" . $request->action(); // 设置 Request::instance()->module('module_name') ``` >## 获取请求参数 ``` $request = Request::instance(); echo '请求方法:' . $request->method() . '<br/>'; // GET echo '资源类型:' . $request->type() . '<br/>'; // html echo '访问ip地址:' . $request->ip() . '<br/>'; // 127.0.0.1 echo '是否AJax请求:' . var_export($request->isAjax(), true) . '<br/>'; // false echo '请求参数:'; dump($request->param()); // array (size=2) 'test' => string 'ddd' (length=3) 'name' => string 'thinkphp' (length=8) echo '请求参数:仅包含name'; dump($request->only(['name'])); // array (size=1) 'name' => string 'thinkphp' (length=8) echo '请求参数:排除name'; dump($request->except(['name'])); // array (size=1) 'test' => string 'ddd' (length=3) ``` >## 获取路由和调度信息 ``` $request = Request::instance(); dump($request->route()); // 路由信息 dump($request->dispatch()); // 调度信息 // 假设路由定义 return [ 'hello/:name' =>['index/hello',[],['name'=>'\w+']], ]; // 假设访问的url为 http://serverName/hello/thinkphp // 结果 路由信息: array (size=4) 'rule' => string 'hello/:name' (length=11) 'route' => string 'index/hello' (length=11) 'pattern' => array (size=1) 'name' => string '\w+' (length=3) 'option' => array (size=0) empty 调度信息: array (size=2) 'type' => string 'module' (length=6) 'module' => array (size=3) 0 => null 1 => string 'index' (length=5) 2 => string 'hello' (length=5) ``` >## 设置请求信息 ``` $request = Request::instance(); $request->root('index.php'); $request->pathinfo('index/index/hello'); ```