Code review of material implementation of the material selection task based on Yii 2.0 RESTful style API
1. The requirements put forward by the new colleagues are required to realize the following requirements:
(1) Logged in, desktop
\api\rests\resource
Modified to:
\API\RESTS\PLAN_TASK_RESOURCE
(2) Tourists, do not make changes
\API\RESTS\CLIENT_RESOURCE
(3) Logged in, mobile terminal
Task Management – Delete Task Materials
Task Management – Manage background material list
\API\RESTS\MOBILE\PLAN_TASK_RESOURCE
(4) Logged in, mobile terminal
/v1/mobile/resources/resource
material upload
\API\RESTS\MOBILE\PLAN_TASK_RESOURCE
2. Code review, \api\config\urlmanager.php, configure:controller=>[‘v1/client-resource’], exists 2, the rule configuration is redundant, delete (client — task management – material)
// 客户端 - 素材
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/client-resource'],
'tokens' => ['{id}' => ''],
'extraPatterns' => [
'POST resource' => 'create',
'POST make-up' => 'makeup',
'POST del-or-recovery' => 'del-or-recovery',
],
],
// 客户端 -- 任务管理 - 素材
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/client-resource'],
'only' => ['index', 'create', 'update', 'edit', 'list', 'delete'],
'tokens' => ['{id}' => ''],
],
3. \API\Config\URLManager.php, configure:controller=>[‘v1/client-resource’], the rules are adjusted as follows (special name and operation name should be consistent, and the ONLY option is configured to explicitly list which behaviors are supported):
Adjust to:
// 客户端 - 素材
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/client-resource'],
'only' => ['resource', 'make-up', 'del-or-recovery'],
'tokens' => ['{id}' => ''],
'extraPatterns' => [
'POST resource' => 'resource',
'POST make-up' => 'make-up',
'POST del-or-recovery' => 'del-or-recovery',
],
],
4. Controller: \api\controllers\clientResourceController.php, the actions() method needs to be adjusted, yii\rest\activecontroller Some actions are provided by default, and if not required, they need to be destroyed. When displaying data through RESTful APIs, it is often necessary to check whether the current user has permission to access and operate the requested resources. =≫ before and after, only a space is required, no alignment is required.
/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
$actions['create'] = [
'class' => 'api\rests\client_resource\ResourceAction',
'modelClass' => $this->modelClass,
];
$actions['makeup'] = [
'class' => 'api\rests\client_resource\ResourceUpdateAction',
'modelClass' => $this->modelClass,
];
$actions['del-or-recovery'] = [
'class' => 'api\rests\client_resource\DelOrRecoveryAction',
'modelClass' => $this->modelClass,
];
return $actions;
}
Adjust to:
/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
// 禁用"index"、"view"、"create"、"update"、"delete"、"options"动作
unset($actions['index'], $actions['view'], $actions['create'], $actions['update'], $actions['delete'], $actions['options']);
$actions['resource'] = [
'class' => 'api\rests\client_resource\ResourceAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
];
$actions['make-up'] = [
'class' => 'api\rests\client_resource\ResourceUpdateAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
];
$actions['del-or-recovery'] = [
'class' => 'api\rests\client_resource\DelOrRecoveryAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
];
return $actions;
}
5. Controller in module V1:\api\modules\v1\controllers\clientResourceController.php, the code comment is not consistent with the controller itself
Adjust to:
6. Directory: The method file in \api\rests\client_resource, you need to rename:
\API\RESTS\CLIENT_RESOURCE\ResourceUpdateAction.php Renamed: \API\RESTS\Client_Resource\MakeUpAction.php
7. Logged in, desktop, \api\config\urlmanager.php, configuration:controller=>[‘v1/plan-task-resource’], the array needs a space after
// 任务管理 - 素材
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/plan-task-resource'],
'only' => ['index','delete','create'],
'tokens' => ['{id}' => ''],
'extraPatterns' => [
'POST resource' => 'create',
],
],
Adjust to:
// 任务管理 - 素材
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/plan-task-resource'],
'only' => ['index', 'delete', 'resource'],
'tokens' => ['{id}' => ''],
'extraPatterns' => [
'POST resource' => 'resource',
],
],
8. Controller: \api\controllers\plantaskResourceController.php, the actions() method needs to be adjusted, yii\rest\activecontroller Some actions are provided by default, and if not required, they need to be destroyed. When displaying data through RESTful APIs, it is often necessary to check whether the current user has permission to access and operate the requested resources. =≫ before and after, only a space is required, no alignment is required.
/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
$actions['delete']['class'] = 'api\rests\plan_task_resource\DeleteAction';
$actions['index']['class'] = 'api\rests\plan_task_resource\IndexAction';
$actions['create'] = [
'class' => 'api\rests\plan_task_resource\ResourceAction',
'modelClass' => $this->modelClass,
];
return $actions;
}
Adjust to:
/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
// 禁用"view"、"create"、"update"、"options"动作
unset($actions['view'], $actions['create'], $actions['update'], $actions['options']);
$actions['delete']['class'] = 'api\rests\plan_task_resource\DeleteAction';
$actions['index']['class'] = 'api\rests\plan_task_resource\IndexAction';
$actions['resource'] = [
'class' => 'api\rests\plan_task_resource\ResourceAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
];
return $actions;
}
9. Controller in module V1: \API\modules\v1\controllers\resourcecontroller.php, the class name is not consistent with the file name, and the code comment is not consistent with the controller itself. IDE prompt: non-controller class files, as shown in Figure 1
\API\modules\v1\controllers\resourcecontroller.php Rename to:\api\modules\v1\controllers\plantaskResourceController.php
Adjust to:
10. Logged in, mobile terminal, \api\config\urlmanager.php, configure:controller=>[‘v1/mobile/plan-task-resource’], the array needs a space, and the code comment is not consistent with the menu structure
// 移动端 - 任务素材
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/mobile/plan-task-resource'],
'only' => ['index','delete','create'],
'tokens' => ['{id}' => ''],
'extraPatterns' => [
'POST resource' => 'create',
],
],
Adjust to:
// 移动端 - 任务 - 素材
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/mobile/plan-task-resource'],
'only' => ['index', 'delete', 'resource'],
'tokens' => ['{id}' => ''],
'extraPatterns' => [
'POST resource' => 'resource',
],
],
11. Controller: \API\Controllers\Mobile\PlantaskResourceController.php, the actions() method needs to be adjusted. Actions() method and public property $serializer, you need to empty a line
'api\rests\mobile\plan_task_resource\Serializer',
'collectionEnvelope' => 'items',
];
/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
$actions['delete']['class'] = 'api\rests\mobile\plan_task_resource\DeleteAction';
$actions['index']['class'] = 'api\rests\mobile\plan_task_resource\IndexAction';
$actions['create']['class'] = 'api\rests\mobile\plan_task_resource\ResourceAction';
return $actions;
}
}
Adjust to:
'api\rests\mobile\plan_task_resource\Serializer',
'collectionEnvelope' => 'items',
];
/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
$actions['delete']['class'] = 'api\rests\mobile\plan_task_resource\DeleteAction';
$actions['index']['class'] = 'api\rests\mobile\plan_task_resource\IndexAction';
$actions['resource']['class'] = 'api\rests\mobile\plan_task_resource\ResourceAction';
return $actions;
}
}
12. Controller: \API\Controllers\Mobile\ResourceController.php, you need to delete
13. Controller in module V1: \API\modules\v1\controllers\mobile\resourcecontroller.php, need to be deleted
14. Directory: The method file in \api\rests\mobile\plan_task_resource, which needs to be adjusted
\API\RESTS\Mobile\Plan_Task_Resource\Action.php
* @since 1.0
*/
class Action extends \api\rests\plan_task_resource\Action
{
}
\API\RESTS\Mobile\Plan_Task_Resource\Action.php
Adjust to:
\API\RESTS\Mobile\Plan_Task_Resource\DeleteAction.php, \API\RESTS\Mobile\Plan_ta sk_resource\indexaction.php, \api\rests\mobile\plan_task_resource\serializer.php 3 files, all need to delete the corresponding comments
\API\RESTS\Mobile\Plan_Task_Resource\ResourceAction.php
\API\RESTS\Mobile\Plan_Task_Resource\ResourceAction.php, the file that is not actually used, the reference needs to be deleted, as shown in Figure 2
Adjust to:
15. The design of routing, in a certain aspect, is a problem, and does not make full use of some of the actions provided by Yii\rest\ActiveController by default. To sum up, it is recommended that colleagues refer to the implementation of other functions. If there are similar problems above, they need to be solved one by one.

