ThinkPHP5学习笔记(6)请求和响应-创新互联
在线视频课程:http://www.kancloud.cn/tpshop/thinkphp5/220692
蒲城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!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 }
'; 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]); } }
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站栏目:ThinkPHP5学习笔记(6)请求和响应-创新互联
转载注明:http://scyanting.com/article/dojjgo.html