在 Yii 2.0 的 RESTful APIs 中,view:返回指定资源的详情,HTTP 动词同时支持 GET 与 POST 的实现

1、在 Postman 中 GET 请求指定资源的详情,响应 200,如图1

在 Postman 中 GET 请求指定资源的详情,响应 200

图1

2、查看路由配置,代码如下

        /* 任务管理 */
        [
            'class' => 'yii/rest/UrlRule',
            'controller' => ['v1/plan-task'],
            'only' => [
                'log-create',
                'index',
                'have',
                'export',
                'have-export',
                'create',
                'view',
                'update',
                'claim',
                'delete',
                'finish',
                'transfer',
                'video-edit',
                'write',
                'write-feed',
                'commit-article',
                'article-review',
                'disable',
                'upload',
                'enable',
                'edit'
            ],
            'tokens' => ['{id}' => '<id://w[//w,:;]*>'],
            'extraPatterns' => [
                'GET have' => 'have',
                'GET export' => 'export',
                'GET have-export' => 'have-export',
                'PUT claim/{id}' => 'claim',
                'PUT finish/{id}' => 'finish',
                'POST log/{id}' => 'log-create',
                'GET {id}' => 'view',
                'GET edit/{id}' => 'edit',
                'PUT update' => 'update',
                'PUT transfer/{id}' => 'transfer',
                'GET video-edit/{id}' => 'video-edit',
                'GET write/{id}' => 'write',
                'GET write-feed/{id}' => 'write-feed',
                'GET commit-article/{id}' => 'commit-article',
                'GET article-review/{id}' => 'article-review',
                'PUT disable/{id}' => 'disable',
                'GET upload/{id}' => 'upload',
                'PUT enable/{id}' => 'enable',
            ],
        ],

3、查看控制器,代码如下

<?php
/**
 * Created by PhpStorm.
 * User: Qiang Wang
 * Date: 2018/05/05
 * Time: 11:43
 */

namespace api/controllers;

use yii/rest/ActiveController;

class PlanTaskController extends ActiveController
{
    public $serializer = [
        'class' => 'api/rests/plan_task/Serializer',
        'collectionEnvelope' => 'items',
    ];
    
    /**
     * @inheritdoc
     */
    public function actions()
    {
        $actions = parent::actions();
        // 禁用"options"动作
        unset($actions['options']);
        $actions['index']['class'] = 'api/rests/plan_task/IndexAction';
        $actions['create']['class'] = 'api/rests/plan_task/CreateAction';
        $actions['view']['class'] = 'api/rests/plan_task/ViewAction';
        $actions['update']['class'] = 'api/rests/plan_task/UpdateAction';
        $actions['delete']['class'] = 'api/rests/plan_task/DeleteAction';
        $actions['have'] = [
            'class' => 'api/rests/plan_task/HaveAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['export'] = [
            'class' => 'api/rests/plan_task/ExportAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['have-export'] = [
            'class' => 'api/rests/plan_task/HaveExportAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['edit'] = [
            'class' => 'api/rests/plan_task/EditAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['claim'] = [
            'class' => 'api/rests/plan_task/ClaimAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['finish'] = [
            'class' => 'api/rests/plan_task/FinishAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['transfer'] = [
            'class' => 'api/rests/plan_task/TransferAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['video-edit'] = [
            'class' => 'api/rests/plan_task/VideoEditAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['write'] = [
            'class' => 'api/rests/plan_task/WriteAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['write-feed'] = [
            'class' => 'api/rests/plan_task/WriteFeedAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['commit-article'] = [
            'class' => 'api/rests/plan_task/CommitArticleAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['article-review'] = [
            'class' => 'api/rests/plan_task/ArticleReviewAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['disable'] = [
            'class' => 'api/rests/plan_task/DisableAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['upload'] = [
            'class' => 'api/rests/plan_task/UploadAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['enable'] = [
            'class' => 'api/rests/plan_task/EnableAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['log-create'] = [
            'class' => 'api/rests/plan_task/LogCreateAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];
        $actions['status-count'] = [
            'class' => 'api/rests/plan_task/StatusCountAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
        ];

        return $actions;
    }
}

4、在 Postman 中 POST 请求指定资源的详情,响应 404,如图2

在 Postman 中 POST 请求指定资源的详情,响应 404

图2

{
    "name": "Not Found",
    "message": "页面未找到。",
    "code": 0,
    "status": 404,
    "type": "yii//web//NotFoundHttpException"
}

5、调整路由配置,GET /plan-tasks/79 和 POST /plan-tasks/79:返回资源ID为 79 的详细信息

'GET,POST {id}' => 'view',

6、在 Postman 中 POST 请求指定资源的详情,响应 405,不允许的方法。该 URL 仅可以处理以下请求方法:GET,HEAD。如图3

在 Postman 中 POST 请求指定资源的详情,响应 405,不允许的方法。该 URL 仅可以处理以下请求方法:GET,HEAD

图3

{
    "name": "Method Not Allowed",
    "message": "Method Not Allowed. This URL can only handle the following request methods: GET, HEAD.",
    "code": 0,
    "status": 405,
    "type": "yii//web//MethodNotAllowedHttpException"
}

7、查看日志,在过滤器 verbFilter:支持 HTTP 方法验证 处验证失败,如图4

查看日志,在过滤器 verbFilter:支持 HTTP 方法验证 处验证失败

图4

yii/web/MethodNotAllowedHttpException: Method Not Allowed. This URL can only handle the following request methods: GET, HEAD. in E:/wwwroot/pcs-api-feature-php-7.4/vendor/yiisoft/yii2/filters/VerbFilter.php:105
Stack trace:
#0 [internal function]: yii/filters/VerbFilter->beforeAction(Object(yii/base/ActionEvent))
#1 E:/wwwroot/pcs-api-feature-php-7.4/vendor/yiisoft/yii2/base/Component.php(627): call_user_func(Array, Object(yii/base/ActionEvent))
#2 E:/wwwroot/pcs-api-feature-php-7.4/vendor/yiisoft/yii2/base/Controller.php(276): yii/base/Component->trigger('beforeAction', Object(yii/base/ActionEvent))
#3 E:/wwwroot/pcs-api-feature-php-7.4/vendor/yiisoft/yii2/web/Controller.php(188): yii/base/Controller->beforeAction(Object(api/rests/plan_task/ViewAction))
#4 E:/wwwroot/pcs-api-feature-php-7.4/vendor/yiisoft/yii2/base/Controller.php(155): yii/web/Controller->beforeAction(Object(api/rests/plan_task/ViewAction))
#5 E:/wwwroot/pcs-api-feature-php-7.4/vendor/yiisoft/yii2/base/Module.php(528): yii/base/Controller->runAction('view', Array)
#6 E:/wwwroot/pcs-api-feature-php-7.4/vendor/yiisoft/yii2/web/Application.php(103): yii/base/Module->runAction('v1/plan-task/vi...', Array)
#7 E:/wwwroot/pcs-api-feature-php-7.4/vendor/yiisoft/yii2/base/Application.php(386): yii/web/Application->handleRequest(Object(yii/web/Request))
#8 E:/wwwroot/pcs-api-feature-php-7.4/api/web/index.php(17): yii/base/Application->run()
#9 {main}

8、在控制器中覆盖 yii/rest/ActiveController 的 verbs 方法,添加对于 POST 的支持

    /**
     * {@inheritdoc}
     */
    protected function verbs()
    {
        $verbs = parent::verbs();
        $verbs['view'] = ['GET', 'HEAD', 'POST'];
        return $verbs;
    }

9、在 Postman 中 GET 或者 POST 请求指定资源的详情,响应 200,如图5

在 Postman 中 GET 或者 POST 请求指定资源的详情,响应 200

图5

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250533.html

(0)
上一篇 2022年4月29日
下一篇 2022年4月29日

相关推荐

发表回复

登录后才能评论