1、要求新来的同事实现时提出的需求如下:
(1)已登录,桌面端
/api/rests/resource
修改为:
/api/rests/plan_task_resource
(2)游客,不做改动
/api/rests/client_resource
(3)已登录,移动端
任务管理 – 删除任务素材
任务管理 – 管理后台素材列表
/api/rests/mobile/plan_task_resource
(4)已登录,移动端
/v1/mobile/resources/resource
素材上传
/api/rests/mobile/plan_task_resource
2、代码审查,/api/config/urlManager.php,配置:’controller’ => [‘v1/client-resource’], 存在 2 处,规则配置冗余,删除(客户端 — 任务管理 – 素材)
// 客户端 - 素材 [ 'class' => 'yii/rest/UrlRule', 'controller' => ['v1/client-resource'], 'tokens' => ['{id}' => '<id://w[//w,:;]*>'], '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}' => '<id://w[//w,:;]*>'], ],
3、/api/config/urlManager.php,配置:’controller’ => [‘v1/client-resource’], 规则调整如下(规范:模式名称与操作名称应保持一致、配置 only 选项来明确列出支持哪些行为):
调整为:
// 客户端 - 素材 [ 'class' => 'yii/rest/UrlRule', 'controller' => ['v1/client-resource'], 'only' => ['resource', 'make-up', 'del-or-recovery'], 'tokens' => ['{id}' => '<id://w[//w,:;]*>'], 'extraPatterns' => [ 'POST resource' => 'resource', 'POST make-up' => 'make-up', 'POST del-or-recovery' => 'del-or-recovery', ], ],
4、控制器:/api/controllers/ClientResourceController.php,actions() 方法需要调整,yii/rest/ActiveController 默认提供一些动作,如果不需要的话,需要销毁。通过 RESTful APIs 显示数据时,经常需要检查当前用户是否有权限访问和操作所请求的资源, 在 yii/rest/ActiveController 中, 可覆盖 checkAccess() 方法来完成权限检查。=> 的前后,仅需要一个空格,无需对齐。
/** * @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; }
调整为:
/** * @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、模块 v1 中的控制器:/api/modules/v1/controllers/ClientResourceController.php,代码注释与控制器本身未保持一致
<?php namespace api/modules/v1/controllers; /** * Menu controller for the `v1` module */ class ClientResourceController extends /api/controllers/ClientResourceController { public $modelClass = 'api/modules/v1/models/Resource'; }
调整为:
<?php namespace api/modules/v1/controllers; /** * ClientResource controller for the `v1` module */ class ClientResourceController extends /api/controllers/ClientResourceController { public $modelClass = 'api/modules/v1/models/Resource'; }
6、目录:/api/rests/client_resource 中的方法文件,需要重命名:
/api/rests/client_resource/ResourceUpdateAction.php 重命名为:/api/rests/client_resource/MakeUpAction.php
7、已登录,桌面端,/api/config/urlManager.php,配置:’controller’ => [‘v1/plan-task-resource’], 数组的 , 后需要一个空格
// 任务管理 - 素材 [ 'class' => 'yii/rest/UrlRule', 'controller' => ['v1/plan-task-resource'], 'only' => ['index','delete','create'], 'tokens' => ['{id}' => '<id://w[//w,:;]*>'], 'extraPatterns' => [ 'POST resource' => 'create', ], ],
调整为:
// 任务管理 - 素材 [ 'class' => 'yii/rest/UrlRule', 'controller' => ['v1/plan-task-resource'], 'only' => ['index', 'delete', 'resource'], 'tokens' => ['{id}' => '<id://w[//w,:;]*>'], 'extraPatterns' => [ 'POST resource' => 'resource', ], ],
8、控制器:/api/controllers/PlanTaskResourceController.php,actions() 方法需要调整,yii/rest/ActiveController 默认提供一些动作,如果不需要的话,需要销毁。通过 RESTful APIs 显示数据时,经常需要检查当前用户是否有权限访问和操作所请求的资源, 在 yii/rest/ActiveController 中, 可覆盖 checkAccess() 方法来完成权限检查。=> 的前后,仅需要一个空格,无需对齐。
/** * @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; }
调整为:
/** * @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、模块 v1 中的控制器:/api/modules/v1/controllers/ResourceController.php,类名与文件名未保持一致,代码注释与控制器本身未保持一致。IDE 提示:非控制器类文件,如图1
/api/modules/v1/controllers/ResourceController.php 重命名为:/api/modules/v1/controllers/PlanTaskResourceController.php
<?php namespace api/modules/v1/controllers; /** * Menu controller for the `v1` module */ class PlanTaskResourceController extends /api/controllers/PlanTaskResourceController { public $modelClass = 'api/modules/v1/models/Resource'; }
调整为:
<?php namespace api/modules/v1/controllers; /** * PlanTaskResource controller for the `v1` module */ class PlanTaskResourceController extends /api/controllers/PlanTaskResourceController { public $modelClass = 'api/modules/v1/models/Resource'; }
10、已登录,移动端,/api/config/urlManager.php,配置:’controller’ => [‘v1/mobile/plan-task-resource’], 数组的 , 后需要一个空格,代码注释未与菜单结构保持一致
// 移动端 - 任务素材 [ 'class' => 'yii/rest/UrlRule', 'controller' => ['v1/mobile/plan-task-resource'], 'only' => ['index','delete','create'], 'tokens' => ['{id}' => '<id://w[//w,:;]*>'], 'extraPatterns' => [ 'POST resource' => 'create', ], ],
调整为:
// 移动端 - 任务 - 素材 [ 'class' => 'yii/rest/UrlRule', 'controller' => ['v1/mobile/plan-task-resource'], 'only' => ['index', 'delete', 'resource'], 'tokens' => ['{id}' => '<id://w[//w,:;]*>'], 'extraPatterns' => [ 'POST resource' => 'resource', ], ],
11、控制器:/api/controllers/mobile/PlanTaskResourceController.php,actions() 方法需要调整。actions() 方法 与 公共属性 $serializer 之间,需要空出一行
<?php namespace api/controllers/mobile; class PlanTaskResourceController extends /api/controllers/PlanTaskResourceController { public $serializer = [ 'class' => '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; } }
调整为:
<?php namespace api/controllers/mobile; class PlanTaskResourceController extends /api/controllers/PlanTaskResourceController { public $serializer = [ 'class' => '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、控制器:/api/controllers/mobile/ResourceController.php,需要删除
13、模块 v1 中的控制器:/api/modules/v1/controllers/mobile/ResourceController.php,需要删除
14、目录:/api/rests/mobile/plan_task_resource 中的方法文件,需要调整
/api/rests/mobile/plan_task_resource/Action.php
<?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace api/rests/mobile/plan_task_resource; /** * Action is the base class for action classes that implement RESTful API. * * For more details and usage information on Action, see the [guide article on rest controllers](guide:rest-controllers). * * @author Qiang Wang <shuijingwanwq@163.com> * @since 1.0 */ class Action extends /api/rests/plan_task_resource/Action { }
/api/rests/mobile/plan_task_resource/Action.php
调整为:
<?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace api/rests/mobile/plan_task_resource; class Action extends /api/rests/plan_task_resource/Action { }
/api/rests/mobile/plan_task_resource/DeleteAction.php、/api/rests/mobile/plan_task_resource/IndexAction.php、/api/rests/mobile/plan_task_resource/Serializer.php 等 3 个文件,皆需要删除对应的注释
/api/rests/mobile/plan_task_resource/ResourceAction.php
<?php namespace api/rests/mobile/plan_task_resource; use Yii; use yii/db/ActiveRecord; use yii/rest/Action; use yii/web/ServerErrorHttpException; use api/models/Resource; use api/models/PlanTask; class ResourceAction extends /api/rests/plan_task_resource/ResourceAction { }
/api/rests/mobile/plan_task_resource/ResourceAction.php,未实际使用的文件,其引用需要删除,如图2
调整为:
<?php namespace api/rests/mobile/plan_task_resource; class ResourceAction extends /api/rests/plan_task_resource/ResourceAction { }
15、路由的设计,从某个方面来说是存在问题的,没有充分利用 yii/rest/ActiveController 默认提供的一些动作,但是,由于接口需要向后兼容,路由已经不好调整,只好沿用下去。总结,建议同事些参考一下其他功能的实现,如果也存在上述类似的问题,皆需要逐一解决。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250631.html