没有不值得去解决的问题,也没有不值得去学习的技术!

Implementing the View Action in Yii 2.0 RESTful APIs: Returning Resource Details With GET and POST Support

In Postman, make a POST request for the details of a specified resource; the response is 405, Method Not Allowed. This URL can only handle the following request methods: GET, HEAD

1. In Postman, make a GET request for the details of a specified resource; the response is 200, as shown in Figure 1.

In Postman, make a GET request for the details of a specified resource; the response is 200
Figure 1

2. View the routing configuration. The code is as follows:

Plaintext
        /* 任务管理 */
        [
            '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}' => ''],
            '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. View the controller. The code is as follows:

PHP
<?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. In Postman, make a POST request for the details of a specified resource; the response is 404, as shown in Figure 2.

In Postman, make a POST request for the details of a specified resource; the response is 404
Figure 2
Plaintext
{
    "name": "Not Found",
    "message": "页面未找到。",
    "code": 0,
    "status": 404,
    "type": "yii\\web\\NotFoundHttpException"
}

5. Adjust the routing configuration. GET /plan-tasks/79 and POST /plan-tasks/79: return the detailed information of the resource with ID 79.

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

6. In Postman, make a POST request for the details of a specified resource; the response is 405, Method Not Allowed. This URL can only handle the following request methods: GET, HEAD. As shown in Figure 3.

In Postman, make a POST request for the details of a specified resource; the response is 405, Method Not Allowed. This URL can only handle the following request methods: GET, HEAD
Figure 3
Plaintext
{
    "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. Check the logs; validation failed at the filter verbFilter: HTTP method validation, as shown in Figure 4.

Check the logs; validation failed at the filter verbFilter: HTTP method validation
Figure 4
Plaintext
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. Override the verbs method of yii\rest\ActiveController in the controller to add support for POST.

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

9. In Postman, make a GET or POST request for the details of a specified resource; the response is 200, as shown in Figure 5.

In Postman, make a GET or POST request for the details of a specified resource; the response is 200
Figure 5

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系