<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # API開發---快速開發API通用接口 > 在在真實開發中 ,很多API接口功能都是邏輯非常簡單 功能也是相似的 為了開發方便 縮短開發周期 > 很多簡單接口都可以通過配置文件來完成 [TOC] 通過實例講解一下實現過程 ### 首先 在基類中創建一個_empty ~~~ protected $api_config=[]; /** * API默認統一操作方法 * Power: Mikkle * Email:776329498@qq.com * @return array */ public function _empty(){ $action = $this->request->action(); $this->api_config=Config::get("api_{$this->request->controller()}"); if (isset($this->api_config[$action])){ $action_name = $this->api_config[$action]['action_name']; //判斷api_config中定義執行的方法是否存在 if( method_exists($this,$action_name)){ return $this->$action_name($action); }else{ return self::showJsonReturnCodeWithOutData(1002); } }else{ return self::showJsonReturnCodeWithOutData(1003,"參數錯誤"); } } ~~~ ### 其次在基類中創建一個通用的修改方法 ~~~ /** * 通用添加修改處理方法 * Power: Mikkle * Email:776329498@qq.com * @param $action //方法名稱 * @return array */ protected function handleEditData($action){ try{ if($this->request->isPost()){ throw new Exception("錯誤的提交方式"); } if( ! isset($this->api_config[$action]['model_name']) ){ throw new Exception("錯誤的處理方式"); } $param_list = isset($this->api_config[$action]['param_list']) ? $this->api_config[$action]['param_list'] : false ; $model_name = $this->api_config[$action]['model_name']; $validate_name = isset($this->api_config[$action]['validate_name']) ? $this->api_config[$action]['validate_name'] : false; return $this->editData($param_list,$validate_name,$model_name); }catch (Exception $e){ Log::error($e->getMessage()); return self::showJsonReturnCodeWithOutData(1008,$e->getMessage()); } } ~~~ 當然 更多方法等你創建 加油 也歡迎大家分享你的通用方法 ### 創建指定的Config配置文件 我們在api模塊下的extra擴展目錄創建指定的config文件即可 在上面的配置中 我是使用這個方法 ~~~ $this->api_config=Config::get("api_{$this->request->controller()}"); ~~~ >[info] 那么我們定義時候的文件名就是api_加上控制器名稱 > 假設我們控制器名稱是Index 我們就創建api_index的cinfig文件 ~~~ <?php /** * Created by PhpStorm. * Power By Mikkle * Email:776329498@qq.com * Date: 2017/11/3 * Time: 20:27 */ return [ "useredit"=>[ 'action_name' => 'handleEditData', 'param_list'=>[ "address"=>"address/s", "contacts"=>"contacts/s", "jobs"=>"jobs/s", "mobile"=>"mobile/s", "tencent_code"=>"tencent_code/s", ], 'model_name' => "base/PersonnelNode", 'validate_name' => 'base/PersonnelNode.edit', ], ]; ~~~ ### 最后就是創建模型和驗證器類即可 ### 接口地址我就不說了 你自己領悟吧 ### 你可以考慮使用Error空控制器 好處 你懂的 ~~~ <?php namespace app\api\controller; /** * Created by PhpStorm. * Power By Mikkle * Email:776329498@qq.com * Date: 2017/10/31 * Time: 20:26 */ use think\Controller; // 空控制器 class Error extends Base { // 空操作 protected $api_config=[]; public function _empty(){ $action = $this->request->action(); $this->api_config=Config::get("api_{$this->request->controller()}"); if (isset($this->config_list[$action])){ $action_name = $this->api_config[$action]['action_name']; //判斷api_config中定義執行的方法是否存在 if( method_exists($this,$action_name)){ return $this->$action_name($action); }else{ return self::showJsonReturnCodeWithOutData(1002); } }else{ return self::showJsonReturnCodeWithOutData(1003,"參數錯誤"); } } } ~~~ ## 本節綜述 >[info] 通過空控制器 空方法 加上配置文件 你甚至可以創建沒有控制器文件的API接口 > API總是五花八門的 更多腦洞等你填滿
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看