扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
在线视频课程:http://www.kancloud.cn/tpshop/thinkphp5/220692
创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、成都网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的湘桥网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
Request请求对象和Response响应对象
$_GET、$_POST、$_REQUEST、$_COOKIE
调用请求对象$request=Request::instance()
$this->request或request()->url();助手函数
input()助手函数
响应格式 json xml 模板渲染
页面跳转 重定向
$this->success $this->error $this->redirect
*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h2{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }'; } public function hello(){ // 获取请求类型 if(Request::instance()->isGet()) {echo '当前为GET请求';} if(Request::instance()->isPost())echo '当前为POST请求'; if(Request::instance()->isPut()) echo '当前为PUT请求'; if(Request::instance()->isDelete()) echo '当前为DELETE请求'; if(Request::instance()->isAjax()) echo '当前为AJAX请求'; if(Request::instance()->isPjax()) echo '当前为PJAX请求'; if(Request::instance()->isMobile()) echo '当前为Mobile请求'; if(Request::instance()->isHead()) echo '当前为HEAD请求'; if(Request::instance()->isPatch()) echo '当前为PATCH请求'; if(Request::instance()->isOptions()) echo '当前为OPTIONS请求'; if(Request::instance()->isCli()) echo '当前为cli'; if(Request::instance()->isCgi()) echo '当前为cgi'; if(request()->isGet()){echo '当前为GET请求';}; // 获取URL信息 $request = Request::instance(); $request = request(); echo 'domain:'.$request->domain().'
'; echo 'file:'.$request->baseFile().'
'; echo 'url:'.$request->url().'
'; echo 'url with domain:'.$request->url(true).'
'; echo 'url without query:'.$request->baseUrl().'
'; echo 'root:'.$request->root().'
'; echo 'root with domain:'.$request->root(true).'
'; echo 'pathinfo:'.$request->pathinfo().'
'; echo 'pathinfo:'.$request->path().'
'; echo 'ext'.$request->ext().'
'; // 设置/获取 模块/控制器/操作名称 $request = Request::instance(); echo '当前模块名称是'.$request->module(); echo '当前控制器名称是'.$request->controller(); echo '当前操作名称是'.$request->action(); Request::instance()->module('module_name'); echo '当前模块名称是'.$request->module(); // 获取请求参数 echo '请求方法:'.$request->method().'
'; echo '资源类型:'.$request->type().'
'; echo '访问地址:'.$request->ip().'
'; echo '是否Ajax请求:'.var_export($request->isAjax(),true); 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 = Request::instance(); $request->root('index.php'); $request->pathinfo('admin/index/hello'); } public function hello2(){ $data = ['name'=>'thinkphp','status'=>1]; // 浏览器安装json插件 return json($data); // return xml($data); // $this->assign('name','渲染 模板'); // return $this->fetch(); } // hello3 // 如果没有 extends Controller 就写: // use \traits\controller\Jump; public function hello3(){ $this->success('正常的页面跳转','hello'); $this->error('错误的页面跳转','hello2'); $this->redirect('http://tp-shop.cn'); } public function hello4(){ // 检测变量是否设置 Request::instance()->has('id','get'); Request::instance()->has('name','post'); input('?get.id'); input('?post.name'); // 变量获取 $request = Request::instance(); echo $request->bind('user_name','张三'); echo $request->user_name; print_r($request->param()); echo '
'; echo $request->param('name'); print_r(input()); echo '
'; echo input('name'); print_r(Request::instance()->param(false)); print_r(Request::instance()->param(true)); echo (input('param.name')); print_r (input('param.')); // 或 echo (input('name')); print_r(''); // 获取GET变量 echo (Request::instance()->get('id')); echo (Request::instance()->get('name')); print_r(Request::instance()->get()); // 获取经过过滤的 print_r(Request::instance()->get(false));//获取原始变量 // 或 echo input('get.id'); echo input('get.name'); print_r(input('get.')); // 获取POST变量 echo (Request::instance()->post('name')); print_r(Request::instance()->post()); print_r(Request::instance()->post(false)); // 或 echo (input('post.name')); print_r('post.'); // 获取PUT变量 echo (Request::instance()->put('name')); print_r (Request::instance()->put()); print_r (Request::instance()->put(false)); // 或 echo (input('put.name')); print_r(input('put.')); // 获取REQUEST变量 echo (Request::instance()->request('id')); print_r(Request::instance()->request()); print_r(Request::instance()->request(false)); // 或 echo (input('request.id')); print_r (input('request.')); // 获取SERVER变量 echo (Request::instance()->server('PHP_SELF')); print_r(Request::instance()->server()); // 或 echo (input('server.PHP_SELF')); print_r(input('server.')); //获取SESSION变量 echo (Request::instance()->session('user_id')); print_r(Request::instance()->session()); // 或 echo (input('session.user_id')); print_r(input('session.')); // 获取Cookie变量 echo (Request::instance()->cookie('user_id')); print_r(Request::instance()->cookie()); // 或 echo (input('cookie.user_id')); print_r(input('cookie.')); // 变量过滤 // 在application/config.php中设置 // 'default_filter'=>'htmlspecialchars'; Request::instance()->filter('htmlspecialchars'); Request::instance()->filter(['strip_tags','htmlspecialchars']); // 在添加变量的时候添加过滤方法 Request::instance()->get('name','','htmlspecialchars'); Request::instance()->param('username','','strip_tags'); Request::instance()->post('name','','org\Filter::safeHtml'); Request::instance()->param('username','','strip_tags,strtolower'); Request::instance()->post('email','','FILTER_VALIDATE_EMAIL'); Request::instance()->post('email','','email'); Request::instance()->get('name','',false); // 获取部分变量 Request::instance()->only('id,name'); Request::instance()->only(['id','name']); Request::instance()->only(['id','name'],'get'); Request::instance()->only(['id','name'],'post'); // 排除部分变量 Request::instance()->except('id,name'); Request::instance()->except(['id','name']); Request::instance()->except(['id','name'],'get'); Request::instance()->except(['id','name'],'post'); // 变量修饰符 input('get.id/d'); input('get.name/s'); input('get.ids/a'); Request::instance()->get('id/d'); } public function hello5(){ Request::instance()->get(['id'=>10]); Request::instance()->post(['name'=>'thinkphp']); // 尽量避免直接修改$_GET,$_POST,也不能直接修改param变量,下面写法无效 // Request::instance()->param(['id'=>10]); } }
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流