In the RESTful APIs of Yii 2.0, View: returns the details of the specified resource, and the HTTP verb supports the implementation of GET and POST at the same time
1. In Postman, the details of the specified resource of the GET request, the response is 200, as shown in Figure 1
2. Check the routing configuration, the code is as follows
/* 任务管理 */
[
'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. Check the controller, the code is as follows
<?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. Details of the specified resource for the POST request in Postman, the response is 404, as shown in Figure 2
{
"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 details of resource ID 79
'GET,POST {id}' => 'view',
6. POST requests the details of the specified resource in Postman, the response 405, the method that is not allowed. The URL can only handle the following request methods: GET, HEAD. as shown in Figure 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. Check the log, the verification failed at the filter VerbFilter: support for HTTP method verification, as shown in Figure 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. Overwrite the verbs method of yii\rest\activecontroller in the controller, and add support for post
/**
* {@inheritdoc}
*/
protected function verbs()
{
$verbs = parent::verbs();
$verbs['view'] = ['GET', 'HEAD', 'POST'];
return $verbs;
}
9. In Postman, the details of the specified resource of the get or POST request are responded to 200, as shown in Figure 5




