Yii3 之请求参数初解

基础框架: yiisoft/app-api
1. 声明一个对象用来接收请求参数:
<?php

namespace App\Common;

use Yiisoft\RequestModel\RequestModel as YiiRequestModel;

class RequestModel extends YiiRequestModel
{
}
2. 接收参数:
<?php

declare(strict_types=1);

namespace App\Blog;

use App\Common\RequestModel;
use Psr\Http\Message\ResponseInterface as Response;
use Yiisoft\DataResponse\DataResponseFactoryInterface;

final class ExampleController
{
    private DataResponseFactoryInterface $responseFactory;

    public function __construct(DataResponseFactoryInterface $responseFactory)
    {
        $this->responseFactory = $responseFactory;
    }

    public function show(RequestModel $request): Response
    {
        return $this->responseFactory->createResponse([
            // 接收路由中的变量
            'path' => [
                'id' => $request->getAttributeValue('attributes.id'),
            ],
            // 接收查询参数
            'query' => [
                'name' => $request->getAttributeValue('query.name'),
            ],
            // 接收请求正文
            'body' => [
                'title' => $request->getAttributeValue('body.title'),
            ],
            // 接收请求header
            'headers' => [
                'Host' => $request->getAttributeValue('headers.Host'),
            ],

            'files' => [],
            'cookie' => [],

            // 当前请求的所有数据
            'request' => $request->getRequestData(),
        ]);
    }
}