sobored/mezzio-rest-helpers 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sobored/mezzio-rest-helpers

最新稳定版本:v1.0.1

Composer 安装命令:

composer require sobored/mezzio-rest-helpers

包简介

Helper classes for building RESTful handlers in Mezzio

README 文档

README

Helpers and routes to help build a RESTful & HAL-compliant API in Mezzio.

Installation

Install this library using composer:

$ composer require sobored/mezzio-rest-helpers

RestDispatchTrait

Use this trait for an easy way to create RESTful responses:

<?php

declare(strict_types=1);

namespace App\Handler;

use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use App\Entity\UserEntity;
use App\TableGateway\UserTableGateway;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use SoBoRed\Mezzio\Rest\Exception\NoResourceFoundException;
use SoBoRed\Mezzio\Rest\RestDispatchTrait;

class GetUserHandler implements RequestHandlerInterface
{
    /**
     * @var UserTableGateway
     */
    private $userTable;

    /**
     * Use REST Helper RestDispatchTrait to gain
     * $this->resourceGenerator, $this->responseFactory,
     * and $this->createResponse()
     */
    use RestDispatchTrait;

    public function __construct(
        UserTableGateway $userTable,
        ResourceGenerator $resourceGenerator,
        HalResponseFactory $responseFactory
    )
    {
        $this->userTable = $userTable;
        $this->resourceGenerator = $resourceGenerator;
        $this->responseFactory = $responseFactory;
    }

    public function handle(ServerRequestInterface $request) : ResponseInterface
    {
        $id = $request->getAttribute('id', false);
        $user = $this->userTable->get((int)$id)->current();

        if (! $user instanceof UserEntity) {
            // Throw a REST Helper Exception for HAL-compliant problem details in response
            throw NoResourceFoundException::create("User with id `{$id}` not found");
        }

        // Return a HAL-compliant response that contains the record requested
        return $this->createResponse($request, $user);
    }
}

Exceptions

SoBoRed/mezzio-rest-helpers comes with helpful exceptions that use mezzio/mezzio-problem-details to produce human-readable responses for API errors. An example response for NoResourceFoundException, used above, would be:

{
  "status": 404,
  "detail": "User with id `22` not found",
  "type": "/api/doc/resource-not-found",
  "title": "Resource not found"
}

Available exceptions

  • Status 400: InvalidParameterException, OutOfBoundsException
  • Status 405: MethodNotAllowedException
  • Status 404: NoResourceFoundException
  • Status 500: RuntimeException

Docs

For the problem details to be HAL-compliant, they should point to some form of documentation that helps the user understand the problem they are encountering. For this reason, mezzio-rest-helpers provides documentation and routes for it, which each exception's type points to.

Available doc routes

  • /api/doc/invalid-parameter
  • /api/doc/method-not-allowed-error
  • /api/doc/resource-not-found
  • /api/doc/parameter-out-of-range
  • /api/doc/runtime-error

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2022-10-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固