zoukankan      html  css  js  c++  java
  • Yii框架实现restful 接口调用,增删改查

    创建模块modules;

    在main.php中配置文件:(1)

    (2)控制器层:

                     

    namespace frontendmodulesv1controllers;
    use frontendmodulesv1modelsFruit;
    use  yii
    estActiveController;
    class FruitController extends ActiveController{
       protected $result=array(
           'code'=>0,
           'data'=>'',
           'error'=>'',
       );
    public $modelClass = 'frontendmodulesv1modelsFruit';
     public function actions(){
         $actions = parent::actions();
         unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
         return $actions;
     }
    //查询单条  get
     public function actionView(){
         $id=Yii::$app->request->get('id');
         $FruitList=Fruit::get($id);
         $this->result['data']=$FruitList;
         return  $this->result;
     }
    //查询全部  get
     public function actionIndex(){
         $FruilList=Fruit::getAll();
         $this->result['data']=$FruilList;
         return $this->result;
     }
     //添加 post
     public function actionCreate(){
         $name=Yii::$app->request->post('name');
         $num=Yii::$app->request->post('num');
         if(empty($name)){
             $this->result['code']=1000;
             return $this->result;
         }if(empty($num)){
             $this->result['code']=1001;
             return $this->result;
         }
         $fruit=array(
             'name'=>$name,
             'num'=>$num
         );
         if (false === Fruit::add($fruit)){
             $this->result['code']=1002;
             $this->result['error']='add is error';
         }
          return $this->result;
     }
    
    //修改 post
     public function actionUpdate(){
         $id=Yii::$app->request->post('id');
         $num=Yii::$app->request->post('num');
         if($id<1){
             $this->result['code']=1001;
             return $this->result;
         }
         if($num<1){
             $this->result['code']=1002;
             return $this->result;
         }
         $params=array(
             'num'=>$num,
         );
         if (false === Fruit::modify($id,$params)){
             $this->result['code']=1012;
             $this->result['error']='update is error';
         }
     }
    //删除 get
     public function actionDelete(){
         $id=Yii::$app->request->get('id');
         if($id<1){
             $this->result['code']=1020;
             return $this->result;
         }
         if (false === Fruit::del($id)){
             $this->result['code']=1022;
             $this->result['error']='delete is error';
         }
         return $this->result;
     }

    (3)模型层
    <?php
    namespace frontendmodulesv1models;
    use Yii;
    class Fruit extends yiiaseModel{
        private static $_tbl = 'fruit';
    //查询所有
        public static function getAll(){
            $db = Yii::$app->db->createCommand('SELECT `id`,`name`,`num` FROM ' . self::$_tbl);
            $result = $db->queryAll();
            if( is_array($result) ){
                return $result;
            }
            return array();
        }
    //查询单条
        public static function get($id){
            $db = Yii::$app->db->createCommand('SELECT `id`,`name`,`num` FROM ' . self::$_tbl . ' WHERE id=:id');
            $result = $db->bindValue(':id', $id)->queryOne();
            if( is_array($result) ){
                return $result;
            }
            return array();
        }
        //添加
        public static function add(array &$params){
           $res=Yii::$app->db->createCommand()->insert(self::$_tbl,[
              'name'=>$params['name'],
              'num'=>$params['num'],
           ])->execute();
           if($res){
               return true;
           }else{
               return false;
           }
        }
    
    //修改
        public  static  function modify($id,array &$params){
            $res=Yii::$app->db->createCommand()->update(self::$_tbl,$params,'id='.$id)->execute();
            if($res===0){
                return false;
            }
            return true;
        }
    //删除
        public static function del($id){
            $res=Yii::$app->db->createCommand()->delete(self::$_tbl,'id='.$id)->execute();
            if($res===0){
                return false;
            }
            return true;
        }
    
    }
    
    (4)模块初始化 
    
    <?php
    namespace frontendmodulesv1;
    class Module extends yiiaseModule{
        public $controllerNamespace = 'frontendmodulesv1controllers';
        public function init(){
            parent::init();
        }
    }
     
     
  • 相关阅读:
    CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接
    解惑|你是否为容器监控操碎了心?
    4 使用smartcar进行仿真
    1 简单的ROS机器人仿真
    1 rbx1 Kinetic 安装和测试
    2020.4.13 Resources
    2. dwa_local_planner_params.yaml 参数意义
    robotics
    PCL 点云处理
    C++异常处理解析
  • 原文地址:https://www.cnblogs.com/chaihtml/p/10436116.html
Copyright © 2011-2022 走看看