在線視頻課程:http://www.kancloud.cn/tpshop/thinkphp5/220692
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的湘橋網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
Request請(qǐng)求對(duì)象和Response響應(yīng)對(duì)象
$_GET、$_POST、$_REQUEST、$_COOKIE
調(diào)用請(qǐng)求對(duì)象$request=Request::instance()
$this->request或request()->url();助手函數(shù)
input()助手函數(shù)
響應(yīng)格式 json xml 模板渲染
頁面跳轉(zhuǎn) 重定向
$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 }:)
ThinkPHP V5
[ V5.0 版本由 isGet()) {echo '當(dāng)前為GET請(qǐng)求';} if(Request::instance()->isPost())echo '當(dāng)前為POST請(qǐng)求'; if(Request::instance()->isPut()) echo '當(dāng)前為PUT請(qǐng)求'; if(Request::instance()->isDelete()) echo '當(dāng)前為DELETE請(qǐng)求'; if(Request::instance()->isAjax()) echo '當(dāng)前為AJAX請(qǐng)求'; if(Request::instance()->isPjax()) echo '當(dāng)前為PJAX請(qǐng)求'; if(Request::instance()->isMobile()) echo '當(dāng)前為Mobile請(qǐng)求'; if(Request::instance()->isHead()) echo '當(dāng)前為HEAD請(qǐng)求'; if(Request::instance()->isPatch()) echo '當(dāng)前為PATCH請(qǐng)求'; if(Request::instance()->isOptions()) echo '當(dāng)前為OPTIONS請(qǐng)求'; if(Request::instance()->isCli()) echo '當(dāng)前為cli'; if(Request::instance()->isCgi()) echo '當(dāng)前為cgi'; if(request()->isGet()){echo '當(dāng)前為GET請(qǐng)求';}; // 獲取URL信息 $request = Request::instance(); $request = request(); echo 'domain:'.$request->domain().'
十年磨一劍 - 為API開發(fā)設(shè)計(jì)的高性能框架
'; 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().'
'; // 設(shè)置/獲取 模塊/控制器/操作名稱 $request = Request::instance(); echo '當(dāng)前模塊名稱是'.$request->module(); echo '當(dāng)前控制器名稱是'.$request->controller(); echo '當(dāng)前操作名稱是'.$request->action(); Request::instance()->module('module_name'); echo '當(dāng)前模塊名稱是'.$request->module(); // 獲取請(qǐng)求參數(shù) echo '請(qǐng)求方法:'.$request->method().'
'; echo '資源類型:'.$request->type().'
'; echo '訪問地址:'.$request->ip().'
'; echo '是否Ajax請(qǐng)求:'.var_export($request->isAjax(),true); echo '請(qǐng)求參數(shù):'; dump($request->param()); echo '請(qǐng)求參數(shù):僅包含name'; dump($request->only(['name'])); echo '請(qǐng)求參數(shù):排除name'; dump($request->except(['name'])); // 獲取路由和調(diào)度信息 echo '路由信息'; dump($request->route()); echo '調(diào)度信息'; dump($request->dispatch()); // 設(shè)置請(qǐng)求信息 $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('正常的頁面跳轉(zhuǎn)','hello'); $this->error('錯(cuò)誤的頁面跳轉(zhuǎn)','hello2'); $this->redirect('http://tp-shop.cn'); } public function hello4(){ // 檢測變量是否設(shè)置 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()); // 獲取經(jīng)過過濾的 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中設(shè)置 // 'default_filter'=>'htmlspecialchars'; Request::instance()->filter('htmlspecialchars'); Request::instance()->filter(['strip_tags','htmlspecialchars']); // 在添加變量的時(shí)候添加過濾方法 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]); } }
分享標(biāo)題:ThinkPHP5學(xué)習(xí)筆記(6)請(qǐng)求和響應(yīng)
URL鏈接:http://weahome.cn/article/gdophg.html