W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
在創(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應(yīng)用中創(chuàng)建操作類似,唯一的差別是Web應(yīng)用中調(diào)用render()
方法渲染一個視圖作為返回值, 對于RESTful操作直接返回數(shù)據(jù),yii\rest\Controller::serializer 和 yii\web\Response 會處理原始數(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()方法中聲明, 可覆蓋該方法來配置單獨的過濾器,禁用某個或增加你自定義的過濾器。 例如,如果你只想用HTTP 基礎(chǔ)認證,可編寫如下代碼:
use yii\filters\auth\HttpBasicAuth;
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
];
return $behaviors;
}
ActiveController
如果你的控制器繼承yii\rest\ActiveController,應(yīng)設(shè)置yii\rest\ActiveController::modelClass 屬性 為通過該控制器返回給用戶的資源類名,該類必須繼承yii\db\ActiveRecord.
yii\rest\ActiveController 默認提供一下操作:
所有這些操作通過yii\rest\ActiveController::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
}
請參考獨立操作類的參考文檔學(xué)習(xí)哪些配置項有用。
通過RESTful APIs顯示數(shù)據(jù)時,經(jīng)常需要檢查當前用戶是否有權(quán)限訪問和操作所請求的資源, 在yii\rest\ActiveController中,可覆蓋yii\rest\ActiveController::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.
* 本方法應(yīng)被覆蓋來檢查當前用戶是否有權(quán)限執(zhí)行指定的操作訪問指定的數(shù)據(jù)模型
* 如果用戶沒有權(quán)限,應(yīng)拋出一個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
// 訪問被拒絕應(yīng)拋出ForbiddenHttpException
}
checkAccess()
?方法默認會被yii\rest\ActiveController默認操作所調(diào)用,如果創(chuàng)建新的操作并想執(zhí)行權(quán)限檢查, 應(yīng)在新的操作中明確調(diào)用該方法。
提示: 可使用Role-Based Access Control (RBAC) 基于角色權(quán)限控制組件實現(xiàn)
checkAccess()
。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: