phapi/endpoint 问题修复 & 功能扩展

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

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

phapi/endpoint

最新稳定版本:1.0.0

Composer 安装命令:

composer require phapi/endpoint

包简介

Contains endpoint class that all Phapi Endpoints should extend. Has built in functionality for handling OPTIONS requests.

README 文档

README

Build status Code Climate Test Coverage

All successful requests (except when caching is used) will be routed and dispatched to an Endpoint that should handle the request and return a response with the requested content.

Installation

The package is installed by default by the Phapi framework. Installing the package to use is separately can be done by using composer:

$ composer require phapi/endpoint:1.*

Basic usage

An endpoint contains methods for handling requests. If an endpoint should support GET requests a get() method must exist. Each method must return the content that should be the response body that is sent to the client. The content must be formatted as an array. Serializers will then serialize the array to the correct format.

<?php

namespace Phapi\Endpoint;

class Blog extends Endpoint
{

  public function get($postid) // $postid is an url param, ex: /blog/{postid:i}/
  {
    return [
      'Title' => 'A Phapi tutorial, part 1',
      'Publish date' => '2015-01-01',
      'Author' => 'John Doe',
      ...
    ];
  }

}

All endpoints (that extends the Endpoint class) has the request, response and dependency injection container available: $this->request, $this->response, $this->container.

Status code

The default status code is 200. The status code can be changed by accessing the Response object and setting a new status code:

<?php
public function get()
{
  // Change status code on the response to a 201 CREATED:
  $this->response = $this->response->withStatusCode(201);
}

Errors and Exceptions

Handling errors should be easy, that's why Phapi has many predefined exceptions. With these exceptions it's just a matter of throwing them to get a nice looking response sent to the client:

<?php

namespace Phapi\Endpoint;

use Phapi\Exception\NotFound;

class Blog extends Endpoint
{

  public function get()
  {
    throw new NotFound(
      'The article you are looking for does not exist.',
      23,
      null,
      'https://example.local/docs/23/'
    );
  }

}

This will result in a response like this (JSON in this example):

{
  "errors": {
    "message": "The article you are looking for does not exist.",
    "code": 23,
    "description": "The URI requested is invalid or the resource requested, such as a user, does not exists. Also returned when the requested format is not supported by the requested method.",
    "link": "https://example.local/docs/23/"
  }
}

Redirects

Redirects should be handled by updating the response object:

<?php
public function get()
{
  // This endpoint has moved permanently
  $this->response = $this->response->withStatusCode(301);
  $this->response = $this->response->withAddedHeader('Location': 'http://example.local/endpoint/new');
}

Head requests

All HEAD requests are automatically handled by Endpoints by checking if a get() method is implemented. If it is implemented, it will call the get() method so that all headers are created but it will ignore the body since HEAD requests should not return a body.

Options requests

Endpoints automatically contains functionality for handling OPTIONS requests by looking at the implemented methods and PHPDoc to set headers and generate a body. The method returns supported content types, allowed methods as well as API documentation (if documentation exists).

Documenting the API is done by using PHPDoc and using tags that starts with @api.

<?php
/**
 * @apiUri /blog/12
 * @apiDescription Retrieve the blogs information like
 *                 id, name and description
 * @apiParams id int
 * @apiResponse id int Blog ID
 * @apiResponse name string The name of the blog
 * @apiResponse description string A description of the blog
 * @apiResponse links string
 *              A list of links
 */
 public function get()
 ...

It's possible to do quite advanced documentation this way.

It might be slightly unusual to return a body on an OPTIONS request but this makes the API self-describing. It makes it possible to browse, use and look at the documentation of the API by actually using the API itself.

Phapi

Endpoint is a Phapi package used by the Phapi Framework.

License

Endpoint is licensed under the MIT License - see the license.md file for details

Contribute

Contribution, bug fixes etc are always welcome.

phapi/endpoint 适用场景与选型建议

phapi/endpoint 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.54k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「endpoint」 「phapi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phapi/endpoint 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phapi/endpoint 我们能提供哪些服务?
定制开发 / 二次开发

基于 phapi/endpoint 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-28