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 string $name
   * @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 https://:8080/users

Run URL

步驟 3 - 現在,以這種方式修改MyUser 模型。

<?php
   namespace app\models;
   use app\components\UppercaseBehavior;
   use Yii;
   /**
   * This is the model class for table "user".
   *
   * @property integer $id
   * @property string $name
   * @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 - 要獲取包含此欄位的資料,請執行https://:8080/users?expand=email

Get Data

自定義操作

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 - 內部伺服器錯誤。

廣告