A-A+

thinkphp5的get和post数据封装的方法介绍(代码)

2019年12月28日 我爱编程 暂无评论

本篇文章给大家带来的内容是关于thinkphp5的get和post数据封装的方法介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

thinkphp5的get和post数据封装

一、view(html页面获取数据)

二、控制器

一、view(html页面获取数据)

  1. <form action="index">
  2.     <input type="text" name="status" value="{$where.nireid}" placeholder="状态" >
  3.     <input type="text" name="atype" value="{$where.atype}" placeholder="类型" >
  4.     <input type="text" name="nireid" value="{$where.nireid}" placeholder="昵称" >
  5.     <button type="submit" > 搜索</button>
  6. </form>

二、控制器

1、正常情况下我们是这样操作的

  1. public function index(){
  2.      $where['status'] =input('get.status');
  3.      $where['atype'] =input('get.atype');
  4.      $where['nireid'] =input('get.nireid');
  5.      $this->assign('where',$where);
  6.      $this->assign(UserExtractModel::systemPage($where));
  7.      return $this->fetch();
  8. }

2、其实我们可以这样做

  1. public function index(){
  2.      $where = self::getMore([
  3.           ['status',''],
  4.           ['atype',''],
  5.           ['nireid',''],
  6.      ],$this->request);
  7.      $this->assign('where',$where);
  8.      $this->assign(UserExtractModel::systemPage($where));
  9.      return $this->fetch();
  10. }
  11. public function getMore($params,Request $request=null,$suffix = false){
  12.      if($request === null) $request = Request::instance();
  13.      $p = [];
  14.      $i = 0;
  15.      foreach ($params as $param){
  16.           if(!is_array($param)) {
  17.                $p[$suffix == true ? $i++ : $param] = $request->get($param);
  18.           }else{
  19.                if(!isset($param[1])) $param[1] = null;
  20.                if(!isset($param[2])) $param[2] = '';
  21.                $name = is_array($param[1]) ? $param[0].'/a' : $param[0];
  22.                $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->get($name,$param[1],$param[2]);
  23.           }
  24.      }
  25.      return $p;
  26. }

(不要忘记use think\Request;)

(post同理)

以上就是thinkphp5的get和post数据封装的方法介绍(代码)的详细内容。

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录