当前位置: 首页 > news >正文

Yii2.0 API实例

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

  • DOCUMENTATION
  •  
  • Guide 2.0
  •  
  • API 2.0
  •  
  • Wiki
  •  
  • Tutorials
  •  
  • Screencasts
  •  
  • Resources
  •  
  • Yii 1.1 Tour
  •  
  • API 1.1

Yii 2.0: Building a REST API in Yii2.0 Follow @yiiframework

report it

  • 9
  • 0

Click to follow15 followers

1.Action index

2.Action View

3.Action Create

4.Action Update

5.Action Delete

6.Action DeleteAll

This is wiki page is useful if you are trying to build a customized REST API in Yii2.0

Note: Yii 2 includes a generic REST feature (ActiveController class) that implements a basic REST API for one of your models that you may be able to easily use and not have to write all the below code. http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

Github:

https://github.com/sirinibin/yii2rest

Note:This example is based on the table: user(id(PK AI),name,age,createdAt,updatedAt)

1.Action index 

Request:
  ========
           URL: api/user/index
        method:GET
params:
                {
                  "page":1,
                  "limit":5,
                  "sort":"id",
                  "order":false,
                  "filter":{}
                      "dateFilter":{"from":"2014-09-04","to":"2014-09-05"}  
                 }
Note1: "page"=>is the current page number
       Note2: "limit"=>no.of records in a single page
       Note3: "sort"=>sort field(ie this can be id,name,age createdAt or updatedAt)
       Note4: "order"=>This can be true/false. true=>ascending order while false=>descending order
       Note5: filter=>is a json object to pass any filter elements. eg:{name:'abc',age:20}

Response:

{
              "status": 1,
              "data": [
                  {
                  "id": 30,
                  "name": "john",
                  "age": 78,
                  "createdAt": "2014-09-05 01:53:31",
                  "updatedAt": "2014-09-05 01:53:51"
                  },
                  {
                  "id": 29,
                  "name": "ben",
                  "age": 23,
                  "createdAt": "2014-09-05 01:53:28",
                  "updatedAt": "2014-09-05 01:54:00"
                  },
                  {
                  "id": 28,
                  "name": "rahul",
                  "age": 72,
                  "createdAt": "2014-09-05 01:53:25",
                  "updatedAt": "2014-09-05 01:54:09"
                  },
                  {
                  "id": 27,
                  "name": "shafeeque",
                  "age": 76,
                  "createdAt": "2014-09-05 01:53:21",
                  "updatedAt": "2014-09-05 01:54:24"
                  },
                  {
                  "id": 26,
                  "name": "sirin",
                  "age": 73,
                  "createdAt": "2014-09-04 19:51:49",
                  "updatedAt": "2014-09-05 01:54:32"
                  }
              ],
              "totalItems": "8"
              }

Action Source code:

public function actionIndex()
    {
 
          $params=$_REQUEST;
          $filter=array();
          $sort="";
 
          $page=1;
          $limit=10;
 
           if(isset($params['page']))
             $page=$params['page'];
 
 
           if(isset($params['limit']))
              $limit=$params['limit'];
 
            $offset=$limit*($page-1);
 
 
            /* Filter elements */
           if(isset($params['filter']))
            {
             $filter=(array)json_decode($params['filter']);
            }
 
             if(isset($params['datefilter']))
            {
             $datefilter=(array)json_decode($params['datefilter']);
            }
 
 
            if(isset($params['sort']))
            {
              $sort=$params['sort'];
         if(isset($params['order']))
        {  
            if($params['order']=="false")
             $sort.=" desc";
            else
             $sort.=" asc";
 
        }
            }
 
 
               $query=new Query;
               $query->offset($offset)
                 ->limit($limit)
                 ->from('user')
                 ->andFilterWhere(['like', 'id', $filter['id']])
                 ->andFilterWhere(['like', 'name', $filter['name']])
                 ->andFilterWhere(['like', 'age', $filter['age']])
                 ->orderBy($sort)
                 ->select("id,name,age,createdAt,updatedAt");
 
           if($datefilter['from'])
           {
            $query->andWhere("createdAt >= '".$datefilter['from']."' ");
           }
           if($datefilter['to'])
           {
            $query->andWhere("createdAt <= '".$datefilter['to']."'");
           }
           $command = $query->createCommand();
               $models = $command->queryAll();
 
               $totalItems=$query->count();
 
          $this->setHeader(200);
 
          echo json_encode(array('status'=>1,'data'=>$models,'totalItems'=>$totalItems),JSON_PRETTY_PRINT);
 
    }
          /* Functions to set header with status code. eg: 200 OK ,400 Bad Request etc..*/      
private function setHeader($status)
  {
 
      $status_header = 'HTTP/1.1 ' . $status . ' ' . $this->_getStatusCodeMessage($status);
      $content_type="application/json; charset=utf-8";
 
      header($status_header);
      header('Content-type: ' . $content_type);
      header('X-Powered-By: ' . "Nintriva <nintriva.com>");
  }
  private function _getStatusCodeMessage($status)
  {
      $codes = Array(
      200 => 'OK',
      400 => 'Bad Request',
      401 => 'Unauthorized',
      402 => 'Payment Required',
      403 => 'Forbidden',
      404 => 'Not Found',
      500 => 'Internal Server Error',
      501 => 'Not Implemented',
      );
      return (isset($codes[$status])) ? $codes[$status] : '';
  }

2.Action View 

Request:
           URL: api/user/view/30
    method:GET

       Note1: "30"=>is the Pk of a record in the user table

Response:

{
                "status": 1,
                "data": {
                    "id": 30,
                    "name": "john",
                    "age": 78,
                    "createdAt": "2014-09-05 01:53:31",
                    "updatedAt": "2014-09-05 01:53:51"
                }
              }

Action Source code:

public function actionView($id)
  {
 
    $model=$this->findModel($id);
 
    $this->setHeader(200);
    echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
 
  } 
  /* function to find the requested record/model */
  protected function findModel($id)
  {
      if (($model = User::findOne($id)) !== null) {
      return $model;
      } else {
 
    $this->setHeader(400);
    echo json_encode(array('status'=>0,'error_code'=>400,'message'=>'Bad request'),JSON_PRETTY_PRINT);
    exit;
      }
  }

3.Action Create 

Request:

            URL:  api/user/create
     method:POST
params:
             {
              "name":"abc",
              "age":20,
            }

Response:

{
                  "status": 1,
                  "data": {
                  "id": 32,
                  "name": "abc",
                  "age": "20",
                  "createdAt": "2014-09-05 02:35:18",
                  "updatedAt": "2014-09-05 02:35:18"
                  }
             }

Action Source code:

public function actionCreate()
  {
 
      $params=$_REQUEST;
 
      $model = new User();
      $model->attributes=$params;
 
 
      if ($model->save()) {
 
      $this->setHeader(200);
      echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
 
      } 
      else
      {
      $this->setHeader(400);
      echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
      }
 
  }

4.Action Update 

Request:

            URL: api/user/update/32


            Note1:"32"=>id(PK) of the record we are going to update


     method: POST

params:

params:
           {
              "name":"efg",
              "age":25,
            }

Response:

{
                  "status": 1,
                  "data": {
                  "id": 32,
                  "name": "efg",
                  "age": "25",
                  "createdAt": "2014-09-05 02:35:18",
                  "updatedAt": "2014-09-05 02:45:55"
                  }
             }

Action Source code:

public function actionUpdate($id)
  {
      $params=$_REQUEST;
 
      $model = $this->findModel($id);
 
      $model->attributes=$params;
 
      if ($model->save()) {
 
      $this->setHeader(200);
      echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
 
      } 
      else
      {
      $this->setHeader(400);
      echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
      }
 
  }

5.Action Delete 

Request:

            URL: api/user/update/32
         method:DELETE 

            Note1:"32"=>id(PK) of the record we are going to delete
Response:
                {
                  "status": 1,
                  "data": {
                  "id": 32,
                  "name": "efg",
                  "age": 20,
                  "createdAt": "2014-09-05 02:40:44",
                  "updatedAt": "2014-09-05 02:40:44"
                  }
              }

Action Source code:

public function actionDelete($id)
  {
      $model=$this->findModel($id);
 
      if($model->delete())
      { 
      $this->setHeader(200);
      echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
 
      }
      else
      {
 
      $this->setHeader(400);
      echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
      }
 
  }

6.Action DeleteAll 

Used to delete multiple records at a time.

       Request:

            URL: api/user/deleteall
         method:POST

params:

{
                       "ids":"[27,28]"
                     }
Note1:"ids"=>a list of id(Pk)'s to be deleted

Response:

{
                "status": 1,
                "data": [
                {
                    "id": 27,
                    "name": "shafeeque",
                    "age": 76,
                    "createdAt": "2014-09-05 01:53:21",
                    "updatedAt": "2014-09-05 01:54:24"
                },
                {
                    "id": 28,
                    "name": "rahul",
                    "age": 72,
                    "createdAt": "2014-09-05 01:53:25",
                    "updatedAt": "2014-09-05 01:54:09"
                }
                ]
            }

Action Source code:

public function actionDeleteall()
  {
      $ids=json_decode($_REQUEST['ids']);
 
      $data=array();
 
      foreach($ids as $id)
      {
    $model=$this->findModel($id);
 
    if($model->delete())
      $data[]=array_filter($model->attributes);
    else
    {
      $this->setHeader(400);
      echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
      return;
    }  
      }
 
      $this->setHeader(200);
      echo json_encode(array('status'=>1,'data'=>$data),JSON_PRETTY_PRINT);
 
  }

7.Behaviour to fileter action methods

public function behaviors()
{
    return [
    'verbs' => [
        'class' => VerbFilter::className(),
        'actions' => [
        'index'=>['get'],
        'view'=>['get'],
        'create'=>['post'],
        'update'=>['post'],
        'delete' => ['delete'],
        'deleteall'=>['post'],
        ],
 
    ]
    ];
}
 
  /* This will execute before  any action */
public function beforeAction($event)
{
    $action = $event->id;
    if (isset($this->actions[$action])) {
    $verbs = $this->actions[$action];
    } elseif (isset($this->actions['*'])) {
    $verbs = $this->actions['*'];
    } else {
    return $event->isValid;
    }
    $verb = Yii::$app->getRequest()->getMethod();
 
  $allowed = array_map('strtoupper', $verbs);
 
  if (!in_array($verb, $allowed)) {
 
    $this->setHeader(400);
    echo json_encode(array('status'=>0,'error_code'=>400,'message'=>'Method not allowed'),JSON_PRETTY_PRINT);
    exit;
 
    }  
 
  return true;  
}

Controller source code:UserController.php

namespace app\modules\api\controllers;
 
use Yii;
use app\models\User;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\db\Query;
 
/**
* UserController implements the CRUD actions for User model.
*/
class UserController extends Controller
{
 
    public function behaviors()
    {
    return [
        'verbs' => [
        'class' => VerbFilter::className(),
        'actions' => [
            'index'=>['get'],
            'view'=>['get'],
            'create'=>['post'],
            'update'=>['post'],
            'delete' => ['delete'],
            'deleteall'=>['post'],
        ],
 
        ]
    ];
    }
 
 
    public function beforeAction($event)
    {
    $action = $event->id;
    if (isset($this->actions[$action])) {
        $verbs = $this->actions[$action];
    } elseif (isset($this->actions['*'])) {
        $verbs = $this->actions['*'];
    } else {
        return $event->isValid;
    }
    $verb = Yii::$app->getRequest()->getMethod();
 
      $allowed = array_map('strtoupper', $verbs);
 
      if (!in_array($verb, $allowed)) {
 
        $this->setHeader(400);
        echo json_encode(array('status'=>0,'error_code'=>400,'message'=>'Method not allowed'),JSON_PRETTY_PRINT);
        exit;
 
    }  
 
      return true;  
    }
 
    /**
    * Lists all User models.
    * @return mixed
    */
    public function actionIndex()
    {
 
          $params=$_REQUEST;
          $filter=array();
          $sort="";
 
          $page=1;
          $limit=10;
 
           if(isset($params['page']))
             $page=$params['page'];
 
 
           if(isset($params['limit']))
              $limit=$params['limit'];
 
            $offset=$limit*($page-1);
 
 
            /* Filter elements */
           if(isset($params['filter']))
            {
             $filter=(array)json_decode($params['filter']);
            }
 
             if(isset($params['datefilter']))
            {
             $datefilter=(array)json_decode($params['datefilter']);
            }
 
 
            if(isset($params['sort']))
            {
              $sort=$params['sort'];
         if(isset($params['order']))
        {  
            if($params['order']=="false")
             $sort.=" desc";
            else
             $sort.=" asc";
 
        }
            }
 
 
               $query=new Query;
               $query->offset($offset)
                 ->limit($limit)
                 ->from('user')
                 ->andFilterWhere(['like', 'id', $filter['id']])
                 ->andFilterWhere(['like', 'name', $filter['name']])
                 ->andFilterWhere(['like', 'age', $filter['age']])
                 ->orderBy($sort)
                 ->select("id,name,age,createdAt,updatedAt");
 
           if($datefilter['from'])
           {
            $query->andWhere("createdAt >= '".$datefilter['from']."' ");
           }
           if($datefilter['to'])
           {
            $query->andWhere("createdAt <= '".$datefilter['to']."'");
           }
           $command = $query->createCommand();
               $models = $command->queryAll();
 
               $totalItems=$query->count();
 
          $this->setHeader(200);
 
          echo json_encode(array('status'=>1,'data'=>$models,'totalItems'=>$totalItems),JSON_PRETTY_PRINT);
 
    }
 
 
    /**
    * Displays a single User model.
    * @param integer $id
    * @return mixed
    */
    public function actionView($id)
    {
 
      $model=$this->findModel($id);
 
      $this->setHeader(200);
      echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
 
    }
 
    /**
    * Creates a new User model.
    * @return json
    */
    public function actionCreate()
    {
 
    $params=$_REQUEST;
 
    $model = new User();
    $model->attributes=$params;
 
 
 
    if ($model->save()) {
 
        $this->setHeader(200);
        echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
 
    } 
    else
    {
        $this->setHeader(400);
        echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
    }
 
    }
 
    /**
    * Updates an existing User model.
    * @param integer $id
    * @return json
    */
    public function actionUpdate($id)
    {
    $params=$_REQUEST;
 
    $model = $this->findModel($id);
 
    $model->attributes=$params;
 
    if ($model->save()) {
 
        $this->setHeader(200);
        echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
 
    } 
    else
    {
        $this->setHeader(400);
        echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
    }
 
    }
 
    /**
    * Deletes an existing User model.
    * @param integer $id
    * @return json
    */
    public function actionDelete($id)
    {
    $model=$this->findModel($id);
 
    if($model->delete())
    { 
        $this->setHeader(200);
        echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
 
    }
    else
    {
 
        $this->setHeader(400);
        echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
    }
 
    }
    /**
    * Deletes an existing multiple User models at a time.
    * @return json
    */
    public function actionDeleteall()
    {
    $ids=json_decode($_REQUEST['ids']);
 
    $data=array();
 
    foreach($ids as $id)
    {
      $model=$this->findModel($id);
 
      if($model->delete())
        $data[]=array_filter($model->attributes);
      else
      {
        $this->setHeader(400);
        echo json_encode(array('status'=>0,'error_code'=>400,'errors'=>$model->errors),JSON_PRETTY_PRINT);
        return;
      }  
    }
 
    $this->setHeader(200);
    echo json_encode(array('status'=>1,'data'=>$data),JSON_PRETTY_PRINT);
 
    }
 
    /**
    * Finds the User model based on its primary key value.
    * If the model is not found, a 404 HTTP exception will be thrown.
    * @param integer $id
    * @return User the loaded model
    */
    protected function findModel($id)
    {
    if (($model = User::findOne($id)) !== null) {
        return $model;
    } else {
 
      $this->setHeader(400);
      echo json_encode(array('status'=>0,'error_code'=>400,'message'=>'Bad request'),JSON_PRETTY_PRINT);
      exit;
    }
    }
 
    private function setHeader($status)
      {
 
      $status_header = 'HTTP/1.1 ' . $status . ' ' . $this->_getStatusCodeMessage($status);
      $content_type="application/json; charset=utf-8";
 
      header($status_header);
      header('Content-type: ' . $content_type);
      header('X-Powered-By: ' . "Nintriva <nintriva.com>");
      }
    private function _getStatusCodeMessage($status)
    {
    // these could be stored in a .ini file and loaded
    // via parse_ini_file()... however, this will suffice
    // for an example
    $codes = Array(
        200 => 'OK',
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
    );
    return (isset($codes[$status])) ? $codes[$status] : '';
    }
}

Happy coding..

Sirin

Yii developer

Total 6 comments

#19710report it

  • 0
  • 0

ajith at 2015/12/24 03:42am

Video upload

hi,

can you tell me how can i upload a video using POST request?

#18696report it

  • 0
  • 0

darioo at 2014/12/13 10:10am

Overried action

It is great tutorial, but how to override action? I've tried it like this just to test it but it doesn't work

public function actionIndex()
{
  echo json_encode(['test'=>'aaa']);
}

#18608report it

  • 0
  • 0

ezekielnoob at 2014/11/30 03:51am

Suggestions

Wouldn't it be better if you utilized DELETE verb for delete all like this:

DELETE /contacts = delete all

#18134report it

  • 0
  • 0

YiiJeka at 2014/09/15 03:24am

I'll just leave this here.

DEMO REST Yii2 with AngularJS

https://github.com/githubjeka/angular-yii2

#18094report it

  • 1
  • 0

sirin k at 2014/09/06 10:17am

yes but

Yes yii is restful by default..but I was trying to override all actions with all possible api features like sort,pagination and filter needed for my angularjs front end.

#18089report it

  • 3
  • 0

zelenin at 2014/09/05 06:18pm

Why so dificult?

Yii2 has native rest api tools.

Leave a comment

Please login to leave your comment.

转载于:https://my.oschina.net/u/2552765/blog/802190

相关文章:

  • 关于Whidbey的东西
  • AFURLRequestSerialization
  • top命令简介
  • 竟然发现在windows2003下的搜索工具不能搜索asp文件中的select文字
  • Android高级之十二讲之内存、电量、卡顿、流量
  • 邮件
  • Linux系统环境变量
  • 事务处理行为
  • LeetCode - Linked List Cycle I II
  • Google实用技巧
  • NOIP2001 一元三次方程求解[导数+牛顿迭代法]
  • InnoDB与MyISAM的区别
  • Windows XP sp2全攻略
  • HTML+CSS基础
  • 类ExampleA继承Exception,类ExampleB继承ExampleA。 有如下代码片断:
  • 【Leetcode】101. 对称二叉树
  • 自己简单写的 事件订阅机制
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • “大数据应用场景”之隔壁老王(连载四)
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • Docker: 容器互访的三种方式
  • Git的一些常用操作
  • JavaScript DOM 10 - 滚动
  • JavaScript设计模式与开发实践系列之策略模式
  • java概述
  • jQuery(一)
  • Octave 入门
  • ucore操作系统实验笔记 - 重新理解中断
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • windows-nginx-https-本地配置
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 二维平面内的碰撞检测【一】
  • 给第三方使用接口的 URL 签名实现
  • 基于组件的设计工作流与界面抽象
  • 力扣(LeetCode)21
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 网络应用优化——时延与带宽
  • 线性表及其算法(java实现)
  • 正则表达式
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • AI算硅基生命吗,为什么?
  • raise 与 raise ... from 的区别
  • 阿里云服务器如何修改远程端口?
  • 组复制官方翻译九、Group Replication Technical Details
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #LLM入门|Prompt#3.3_存储_Memory
  • (1)(1.9) MSP (version 4.2)
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627