本篇文章給大家帶來的內容是關于thinkphp5的get和post數據封裝的方法介紹(代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
thinkphp5的get和post數據封裝
一、view(html頁面獲取數據)
二、控制器
一、view(html頁面獲取數據)
- <form action="index">
- <input type="text" name="status" value="{$where.nireid}" placeholder="狀態" >
- <input type="text" name="atype" value="{$where.atype}" placeholder="類型" >
- <input type="text" name="nireid" value="{$where.nireid}" placeholder="昵稱" >
- <button type="submit" > 搜索</button>
- </form>
二、控制器
1、正常情況下我們是這樣操作的
- public function index(){
- $where['status'] =input('get.status');
- $where['atype'] =input('get.atype');
- $where['nireid'] =input('get.nireid');
- $this->assign('where',$where);
- $this->assign(UserExtractModel::systemPage($where));
- return $this->fetch();
- }
2、其實我們可以這樣做
- public function index(){
- $where = self::getMore([
- ['status',''],
- ['atype',''],
- ['nireid',''],
- ],$this->request);
- $this->assign('where',$where);
- $this->assign(UserExtractModel::systemPage($where));
- return $this->fetch();
- }
- public function getMore($params,Request $request=null,$suffix = false){
- if($request === null) $request = Request::instance();
- $p = [];
- $i = 0;
- foreach ($params as $param){
- if(!is_array($param)) {
- $p[$suffix == true ? $i++ : $param] = $request->get($param);
- }else{
- if(!isset($param[1])) $param[1] = null;
- if(!isset($param[2])) $param[2] = '';
- $name = is_array($param[1]) ? $param[0].'/a' : $param[0];
- $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->get($name,$param[1],$param[2]);
- }
- }
- return $p;
- }
(不要忘記use think/Request;)
(post同理)
以上就是thinkphp5的get和post數據封裝的方法介紹(代碼)的詳細內容。
新聞熱點
疑難解答
圖片精選