Yii 字段
通过重写 fields() 和 extraFields() 方法,可以定义哪些数据可以放入响应中。这两个方法的区别在于,前者定义了应该包含在响应中的默认字段集,而后者定义了附加字段,如果终端用户通过 expand 查询参数请求它们,则可能包含在响应中。
步骤1 - 修改 MyUser 模型的方法如下。
<?php
   namespace app\models;
   use app\components\UppercaseBehavior;
   use Yii;
   /**
   * This is the model class for table "user".
   *@property integer id
   * @property stringname
   * @property string email
   */
   class MyUser extends \yii\db\ActiveRecord {
      public function fields() {
         return [
            'id',
            'name',
            //PHP callback
            'datetime' => function(model) {
               return date("d:m:Y H:i:s");
            }
         ];
      }
      /**
      * @inheritdoc
      */
      public static function tableName() {
         return 'user';
      }
      /**
      * @inheritdoc
      */
      public function rules() {
         return [
            [['name', 'email'], 'string', 'max' => 255]
         ];
      }
      /**
      * @inheritdoc
      */
      public function attributeLabels() {
         return [
            'id' => 'ID',
            'name' => 'Name',
            'email' => 'Email',
         ];
      }
   }
?>
除了默认的字段:id和name,我们添加了一个自定义字段 – datetime .
步骤2 - 在Postman中运行URL http://localhost:8080/users .

步骤3 − 现在,按照以下方式修改 MyUser 模型。
<?php
   namespace app\models;
   use app\components\UppercaseBehavior;
   use Yii;
   /**
   * This is the model class for table "user".
   *
   * @property integer id
   * @property stringname
   * @property string $email
   */
   class MyUser extends \yii\db\ActiveRecord {
      public function fields() {
         return [
            'id',
            'name',
         ];
      }
      public function extraFields() {
         return ['email'];
      }
      /**
      * @inheritdoc
      */
      public static function tableName() {
         return 'user';
      }
      /**
      * @inheritdoc
      */
      public function rules() { 
         return [
            [['name', 'email'], 'string', 'max' => 255]
         ];
      }
      /**
      * @inheritdoc
      */
      public function attributeLabels() { 
         return [
            'id' => 'ID',
            'name' => 'Name',
            'email' => 'Email',
         ];
      }
   } 
?>
注意,email字段是通过extraFields()方法返回的。
步骤4 - 要获得带有此字段的数据,请运行 http://localhost:8080/users?expand=email 。

自定义操作
The yii\rest\ActiveController 类提供以下操作:
- Index - 分页列出资源
 - 
View - 返回指定资源的详细信息
 - 
Create - 创建一个新的资源
 - 
Update - 更新一个现有的资源
 - 
Delete - 删除指定的资源
 - 
Options - 返回支持的HTTP方法
 
所有上述操作都在actions方法中声明。
要禁用“delete”和“create”操作,请按以下方式修改 UserController :
<?php
   namespace app\controllers;
   use yii\rest\ActiveController;
   class UserController extends ActiveController {
      public modelClass = 'app\models\MyUser';
      public function actions() {actions = parent::actions();
         // disable the "delete" and "create" actions
         unset(actions['delete'],actions['create']);
         return $actions;
      }
   }
?>
处理错误
在获取RESTful API请求时,如果请求出现错误或服务器发生意外情况,您可以简单地抛出异常。如果您可以确定错误的原因,应该抛出异常以及适当的HTTP状态码。Yii REST使用以下状态码:
- 
200 - OK。
 - 
201 -成功响应POST请求中的资源创建。Location头包含指向新创建资源的URL。
 - 
204 -请求成功处理,响应中不包含内容。
 - 
304 -资源未修改。
 - 
400 -错误的请求。
 - 
401 -身份验证失败。
 - 
403 -认证用户不允许访问指定的API端点。
 - 
404 -资源不存在。
 - 
405 -不允许的方法。
 - 
415 -不支持的媒体类型。
 - 
422 -数据验证失败。
 - 
429 -请求太多。
 - 
500 -内部服务器错误。
 
极客教程