Yii2 REST API 按字段查找
Yii2 REST API find by field
这是apiurl。
/api/web/v1/users/123
按 ID 查找用户。如何更改规则以通过 token
查找,而不是通过 id
查找?
规则如下:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/users'],
'tokens' => [
'{id}' => '<id:\w+>'
// this does not work
// '{token}' => '<token:\w+>'
],
],
定义为令牌的 {id}
是内置 yii\rest\ViewAction 使用的令牌,代码是这样的:
class ViewAction extends Action
{
/**
* Displays a model.
* @param string $id the primary key of the model.
* @return \yii\db\ActiveRecordInterface the model being displayed
*/
public function run($id)
{
$model = $this->findModel($id);
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}
return $model;
}
}
$this->findModel($id)
在 yii\rest\Action 下定义,它使用主键查找模型。如果您需要使用不同的标记,如 {token}
并在与其主键不同的属性中找到您的模型,那么您需要覆盖控制器内的视图操作。
这是一个在您的规则中添加 '{token}' => '<token:\w+>'
时应该起作用的示例:
class UserController extends ActiveController
{
...
public function actions()
{
$actions = parent::actions();
unset($actions['view']);
return $actions;
}
public function actionView($token){
$modelClass = $this->modelClass;
$model = $modelClass::find()->where(['token' => $token])->one();
if ($model === null)
throw new \yii\web\NotFoundHttpException("Uknown user having $token as token");
return $model;
}
}
另请注意,您需要更改 $patterns 以支持新推出的版本。您的最终规则可能如下所示:
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/users'],
'tokens' => [
'{id}' => '<id:\w+>',
'{token}' => '<token:\w+>'
]
'patterns' => [
'PUT,PATCH {id}' => 'update',
'DELETE {id}' => 'delete',
// {token} is assigned to $token when redirecting to view
'GET,HEAD {token}' => 'view',
'POST' => 'create',
'GET,HEAD' => 'index',
'{id}' => 'options',
'' => 'options',
],
],
]
这是apiurl。
/api/web/v1/users/123
按 ID 查找用户。如何更改规则以通过 token
查找,而不是通过 id
查找?
规则如下:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/users'],
'tokens' => [
'{id}' => '<id:\w+>'
// this does not work
// '{token}' => '<token:\w+>'
],
],
定义为令牌的 {id}
是内置 yii\rest\ViewAction 使用的令牌,代码是这样的:
class ViewAction extends Action
{
/**
* Displays a model.
* @param string $id the primary key of the model.
* @return \yii\db\ActiveRecordInterface the model being displayed
*/
public function run($id)
{
$model = $this->findModel($id);
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}
return $model;
}
}
$this->findModel($id)
在 yii\rest\Action 下定义,它使用主键查找模型。如果您需要使用不同的标记,如 {token}
并在与其主键不同的属性中找到您的模型,那么您需要覆盖控制器内的视图操作。
这是一个在您的规则中添加 '{token}' => '<token:\w+>'
时应该起作用的示例:
class UserController extends ActiveController
{
...
public function actions()
{
$actions = parent::actions();
unset($actions['view']);
return $actions;
}
public function actionView($token){
$modelClass = $this->modelClass;
$model = $modelClass::find()->where(['token' => $token])->one();
if ($model === null)
throw new \yii\web\NotFoundHttpException("Uknown user having $token as token");
return $model;
}
}
另请注意,您需要更改 $patterns 以支持新推出的版本。您的最终规则可能如下所示:
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/users'],
'tokens' => [
'{id}' => '<id:\w+>',
'{token}' => '<token:\w+>'
]
'patterns' => [
'PUT,PATCH {id}' => 'update',
'DELETE {id}' => 'delete',
// {token} is assigned to $token when redirecting to view
'GET,HEAD {token}' => 'view',
'POST' => 'create',
'GET,HEAD' => 'index',
'{id}' => 'options',
'' => 'options',
],
],
]