多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 请求信息 namespace app\index\controller; use \think\Request; class Index { //声明请求对象 public function hello() { //使用Request类 $request = Request::instance(); //建议使用助手函数 $request = request(); return $request->param(); } //需要时,建议直接注入请求对象 public function hello1(Request $request) { //获取URL信息 echo '当前域名: ' . $request->domain() . '<br/>'; echo '当前入口文件: ' . $request->baseFile() . '<br/>'; echo '当前URL地址 不含域名: ' . $request->url() . '<br/>'; echo '包含域名的完整URL地址: ' . $request->url(true) . '<br/>'; echo '当前URL地址 不含QUERY_STRING: ' . $request->baseUrl() . '<br/>'; echo 'URL访问的ROOT地址:' . $request->root() . '<br/>'; echo 'URL访问的ROOT地址含域名: ' . $request->root(true) . '<br/>'; echo 'URL地址中的PATH_INFO信息: ' . $request->pathinfo() . '<br/>'; echo 'URL地址中的PATH_INFO信息 不含后缀: ' . $request->path() . '<br/>'; echo 'URL地址中的后缀信息: ' . $request->ext() . '<br/>'; //设置和获取模块/控制器/操作名称 echo "当前模块名称是" . $request->module(); echo "当前控制器名称是" . $request->controller(); echo "当前操作名称是" . $request->action(); $request->module('module_name'); //设置模块名称 $request->controller('controller_name'); //设置控制器名称 $request->action('action_name'); //设置操作名称 //获取请求参数 echo '请求方法:' . $request->method() . '<br/>'; echo '资源类型:' . $request->type() . '<br/>'; echo '访问地址:' . $request->ip() . '<br/>'; echo '是否AJax请求:' . var_export($request->isAjax(), true) . '<br/>'; echo '请求参数:'; dump($request->param()); echo '请求参数:仅包含name'; dump($request->only(['name'])); echo '请求参数:排除name'; dump($request->except(['name'])); //获取路由和调度信息 echo '路由信息:'; dump($request->route()); echo '调度信息:'; dump($request->dispatch()); //设置请求信息 $request->root('index.php'); $request->pathinfo('index/index/hello'); return ''; } } ## 输入变量 public function hello(Request $request) { //变量检测: has('变量名','变量类型') $request->has('id', 'get'); input('?get.id'); //使用助手函数 //获取变量: 变量类型方法('变量名/变量修饰符','默认值','过滤方法') $request->param('id'); input('param.id'); //使用助手函数 //变量过滤:过滤方式包括函数、方法过滤,以及PHP内置的Types of filters //采用Filter ID 进行过滤的话,如果不符合过滤要求的话会返回false, //因此你需要配合默认值来确保最终的值符合你的规范。 $request->filter(['htmlspecialchars']); //设置全局过滤 $request->param('username', '', 'strip_tags,strtolower'); //获取变量时指定过滤方法 $request->post('email', '', FILTER_VALIDATE_EMAIL); //PHP内置提供的Filter ID过滤 $request->post('email', '', 'email'); //框架对Filter ID做了转换 $request->get('name', '', 'strtolower', true); //配合全局过滤方法 //获取部分变量 $request->only('id,name', 'get'); $request->only(['id','name'], 'post'); //排除部分变量 $request->except('id,name', 'get'); $request->except(['id','name'], 'post'); //变量修饰符 //引用方法: 变量类型方法('变量名/修饰符') $request->get('id/d'); input('param.id/d'); //使用助手函数 return $request->param(); } > 变量类型: > |方法|描述| > |---|---| > |param |获取当前请求的变量| > |get |获取 $_GET 变量| > |post |获取 $_POST 变量| > |put |获取 PUT 变量| > |delete |获取 DELETE 变量| > |session |获取 $_SESSION 变量| > |cookie |获取 $_COOKIE 变量| > |request |获取 $_REQUEST 变量| > |server |获取 $_SERVER 变量| > |env |获取 $_ENV 变量| > |route |获取 路由(包括PATHINFO) 变量| > |file |获取 $_FILES 变量| > 变量修饰符: > |修饰符|作用| > |---|---| > |s |强制转换为字符串类型,TP5默认| > |d |强制转换为整型类型| > |b |强制转换为布尔类型| > |a |强制转换为数组类型| > |f |强制转换为浮点类型| ## 更改变量 public function hello(Request $request) { //修改输入变量 //尽量避免直接修改$_GET 或者 $_POST数据 //不能直接修改param变量 $request->get(['id'=>10]); $request->post(['name'=>'thinkphp']); $request->param(['name'=>'thinkphp']); //此操作无效 return ''; } ## 请求类型 public function hello(Request $request) { if ($request->isGet()) echo "当前为 GET 请求"; if ($request->isPost()) echo "当前为 POST 请求"; if ($request->isPut()) echo "当前为 PUT 请求"; if ($request->isDelete()) echo "当前为 DELETE 请求"; if ($request->isAjax()) echo "当前为 Ajax 请求"; if ($request->isPjax()) echo "当前为 Pjax 请求"; if ($request->isMobile()) echo "当前为手机访问"; if ($request->isHead()) echo "当前为 HEAD 请求"; if ($request->isPatch()) echo "当前为 PATCH 请求"; if ($request->isOptions()) echo "当前为 OPTIONS 请求"; if ($request->isCli()) echo "当前为 cli"; if ($request->isCgi()) echo "当前为 cgi"; return ''; } ## 请求伪装 //将POST请求伪装成PUT请求 //合法的请求类型包括GET、POST、PUT和DELETE等 <form method="post" action=""> <input type="text" name="name" value="Hello"> <input type="hidden" name="_method" value="PUT" > <input type="submit" value="提交"> </form> //修改配置文件 //表单请求类型伪装变量 'var_method' => '_m', //默认值为'_mothod',如上 ## 方法参数绑定 方法参数绑定是把URL地址(或者路由地址)中的变量作为操作方法的参数直接传入。 控制器方法如下: public function read($id) { return 'id='.$id; } public function archive($year='2016',$month='01') { return 'year='.$year.'&month='.$month; } 按名称绑定,当请求URL地址是: //URL中参数名和方法参数名必须一致,顺序可以调整 http://serverName/index.php/index/blog/read/id/5 http://serverName/index.php/index/blog/archive/month/06/year/2016 //方法中参数如果没有默认值,URL访问时必须带此参数,否则会报错 http://serverName/index.php/index/blog/read //页面会返回错误 输出结果为: id=5 year=2016&month=06 按顺序绑定, 合理规划URL参数的顺序绑定可以简化URL地址。 修改配置参数: // URL参数方式改成顺序解析 'url_param_type' => 1, 当请求URL地址是: //URL参数的顺序要与方法中参数顺序一致 http://serverName/index.php/index/blog/archive/2016/06 http://serverName/index.php/index/blog/archive/06/2016 输出结果为: year=2016&month=06 year=06&month=2016 > ## 注意 > 按顺序绑定参数的话,操作方法的参数只能使用URL pathinfo变量,而不能使用get或者post变量。 > 参数绑定有一个特例,如果你的操作方法中定义有Request对象作为参数的话,无论参数位置在哪里,都会自动注入,而不需要进行参数绑定。 ## 注入请求对象 控制器的操作方法中如果需要调用请求对象Request的话,可以在方法中定义Request类型的参数,并且参数顺序无关。 如果继承了系统的Controller类的话,也可以直接调用request属性。 namespace app\index\controller; use think\Controller; class Index extends Controller { public function hello() { return 'Hello,'.$this->request->param('name'); } } ## 获取HTTP头信息 HTTP请求头信息的名称不区分大小写。 namespace app\index\controller; use think\Controller; class Index extends Controller { public function hello() { $info = $this->request->header(); $agent = $this->request->header('user-agent'); echo $info['accept']; echo $info['accept-encoding']; echo $info['user-agent']; echo $agent; } } ## 方法注入 // 通过hook方法注入动态方法 Request::hook('user','getUserInfo'); //getUserInfo函数定义如下 function getUserInfo(Request $request, $userId) { // 根据$userId获取用户信息 return $info; } //接下来,我们可以直接在控制器中使用 public function index() { $info = Request::instance()->getUserInfo($userId); } ## 属性注入 // 动态绑定属性 Request::instance()->bind('user',new User); // 或者使用 Request::instance()->user = new User; //获取绑定的属性使用下面的方式: Request::instance()->user; $this->request->user; //如果继承了系统的Controller类的话 request()->user; ## 伪静态 URL伪静态通常是为了满足更好的SEO效果。 修改配置参数 // 打开伪静态后缀访问 'url_html_suffix' => ture, // 后缀类型 'url_html_suffix' => 'shtml|html', //默认值为'html' 按照上面的配置,以下访问是有效: http://serverName/index/blog/3 http://serverName/index/blog/3.html http://serverName/index/blog/3.shtml 按归上面的配置,以下访问是无效的: http://serverName/index/blog/3.xml http://serverName/index/blog/3.pdf 在控制器的操作方法中获取当前访问的伪静态后缀: $ext = Request::instance()->ext();