在創(chuàng)建資源類和指定資源格輸出式化后,下一步就是創(chuàng)建控制器操作將資源通過 RESTful APIs 展現(xiàn)給終端用戶。
Yii 提供兩個控制器基類來簡化創(chuàng)建 RESTful 操作的工作:[[yii\rest\Controller]] 和 [[yii\rest\ActiveController]],兩個類的差別是后者提供一系列將資源處理成Active Record的操作。因此如果使用Active Record內(nèi)置的操作會比較方便,可考慮將控制器類繼承[[yii\rest\ActiveController]],它會讓你用最少的代碼完成強大的 RESTful APIs.
[[yii\rest\Controller]] 和 [[yii\rest\ActiveController]] 提供以下功能,一些功能在后續(xù)章節(jié)詳細描述:
[[yii\rest\ActiveController]] 額外提供一下功能:
index
, view
, create
, update
, delete
, options
;當創(chuàng)建一個新的控制器類,控制器類的命名最好使用資源名稱的單數(shù)格式,例如,提供用戶信息的控制器可命名為UserController
.
創(chuàng)建新的操作和 Web 應用中創(chuàng)建操作類似,唯一的差別是 Web 應用中調(diào)用render()
方法渲染一個視圖作為返回值,對于 RESTful 操作直接返回數(shù)據(jù),[[yii\rest\Controller::serializer|serializer]] 和[[yii\web\Response|response object]] 會處理原始數(shù)據(jù)到請求格式的轉(zhuǎn)換,例如
public function actionView($id)
{
return User::findOne($id);
}
[[yii\rest\Controller]]提供的大多數(shù) RESTful API 功能通過過濾器實現(xiàn).特別是以下過濾器會按順序執(zhí)行:
這些過濾器都在[[yii\rest\Controller::behaviors()|behaviors()]]方法中聲明,可覆蓋該方法來配置單獨的過濾器,禁用某個或增加你自定義的過濾器。例如,如果你只想用 HTTP 基礎認證,可編寫如下代碼:
use yii\filters\auth\HttpBasicAuth;
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
];
return $behaviors;
}
ActiveController
如果你的控制器繼承[[yii\rest\ActiveController]],應設置[[yii\rest\ActiveController::modelClass||modelClass]] 屬性為通過該控制器返回給用戶的資源類名,該類必須繼承[[yii\db\ActiveRecord]].
[[yii\rest\ActiveController]] 默認提供一下操作:
所有這些操作通過[[yii\rest\ActiveController::actions()|actions()]] 方法申明,可覆蓋actions()
方法配置或禁用這些操作,如下所示:
public function actions()
{
$actions = parent::actions();
// 禁用"delete" 和 "create" 操作
unset($actions['delete'], $actions['create']);
// 使用"prepareDataProvider()"方法自定義數(shù)據(jù) provider
$actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];
return $actions;
}
public function prepareDataProvider()
{
// 為"index"操作準備和返回數(shù)據(jù) provider
}
請參考獨立操作類的參考文檔學習哪些配置項有用。
通過 RESTful APIs 顯示數(shù)據(jù)時,經(jīng)常需要檢查當前用戶是否有權(quán)限訪問和操作所請求的資源,在[[yii\rest\ActiveController]]中,可覆蓋[[yii\rest\ActiveController::checkAccess()|checkAccess()]]方法來完成權(quán)限檢查。
/**
* Checks the privilege of the current user. 檢查當前用戶的權(quán)限
*
* This method should be overridden to check whether the current user has the privilege
* to run the specified action against the specified data model.
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
* 本方法應被覆蓋來檢查當前用戶是否有權(quán)限執(zhí)行指定的操作訪問指定的數(shù)據(jù)模型
* 如果用戶沒有權(quán)限,應拋出一個[[ForbiddenHttpException]]異常
*
* @param string $action the ID of the action to be executed
* @param \yii\base\Model $model the model to be accessed. If null, it means no specific model is being accessed.
* @param array $params additional parameters
* @throws ForbiddenHttpException if the user does not have access
*/
public function checkAccess($action, $model = null, $params = [])
{
// 檢查用戶能否訪問 $action 和 $model
// 訪問被拒絕應拋出 ForbiddenHttpException
}
checkAccess()
方法默認會被[[yii\rest\ActiveController]]默認操作所調(diào)用,如果創(chuàng)建新的操作并想執(zhí)行權(quán)限檢查,應在新的操作中明確調(diào)用該方法。
提示: 可使用Role-Based Access Control (RBAC) 基于角色權(quán)限控制組件實現(xiàn)
checkAccess()
。