adamstipak/nette-rest-route 问题修复 & 功能扩展

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

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

adamstipak/nette-rest-route

Composer 安装命令:

composer require adamstipak/nette-rest-route

包简介

Rest route for Nette Framework

关键字:

README 文档

README

Route automatically maps CRUD to Presenters and actions in the defined module. And creates parameters which are accessible in Presenter.

Requirements

  • PHP 8.1+

  • Nette 3.2+

  • format

  • id (autodetected)

  • associations (an array with associations)

  • data (raw data from the request)

  • query (an array of items from the query string)

Format detection:

Variable $format is detected from HTTP header Accept. If header is not present Route try detect format from the URL (.../foo.json). If no format is in the URL Route use a default format json.

Installation:

The best way to install Nette-RestRoute is using Composer:

$ composer require adamstipak/nette-rest-route

Usage:

use AdamStipak\RestRoute;

// $router is an instance of Nette\Application\Routers\RouteList  

// No parameters needed. Presenter name will be generated.
$router[] = new RestRoute;

// With module.
$router[] = new RestRoute('Api');

// With module and xml as a default format.
$router[] = new RestRoute('Api', 'xml');

// With URL module versioning
$router[] = (new RestRoute())
    ->useURLModuleVersioning(
        '/v[0-9\.]+/',     // Regex for URL version
        [                  // URL fragment to module mapping
          NULL => 'V1',    // Default version module is V1
          'v1' => 'V1',    // /v1 to module V1
          'v2' => 'V2'     // /v2 to module V2
        ]
    );

First parameter is a name of the module where the route will sends an Request. URL prefix will be generated. See examples. ####Examples:

NULL      => /<generated presenter name>
'Api'     => /api/<generated presenter name>
'My:Api'  => /my/api/<generated presenter name>
...

Second parameter is default format. By default the default format is json. RestRoute support only 2 formats:

  • json (default)
  • xml

Examples

Read all:

URL: /api/users\ApiModule\UsersPresenter::actionReadAll
HTTP HEADER Accept: application/json
Method: GET
Request body: Empty
Params:

format = json
associations = array(0)
data = ""
query = array(0)

Flag readAll was dropped and Route automatically generate action readAll if no Resource ID was not found in the URL.

Read with resource ID

URL: /api/users/123\ApiModule\UsersPresenter::actionRead
HTTP HEADER Accept: application/json
Method: GET
Request body: Empty
Params:

format = json
id = 123
associations = array(0)
data = ""
query = array(0)

Query params:

URL: /api/users?foo=bar&page=1\ApiModule\UsersPresenter::actionReadAll
HTTP HEADER Accept: application/json
Method: GET
Request body: Empty
Params:

format = json
associations = array(0)
data = ""
query = array(
	foo => "bar"
	page => 1
)

Create:

URL: /api/users\ApiModule\UsersPresenter::actionCreate
HTTP HEADER Accept: application/json
Method: POST
Request body:

{
	"foo": "bar",
	"nested": {
		"foo": "bar"	
	}
}

Params:

format = json
associations = array(0)
data = {"foo": "bar", "nested": {"foo": "bar"}}
query = array(0)

Update:

URL: /api/users/123\ApiModule\UsersPresenter::actionUpdate
HTTP HEADER Accept: application/json
Method: PUT
Request body:

{
	"foo": "bar",
	"nested": {
		"foo": "bar"	
	}
}

Params:

format = json
id = 123
associations = array(0)
data = {"foo": "bar", "nested": {"foo": "bar"}}
query = array(0)

Partial update:

URL: /api/users/123\ApiModule\UsersPresenter::actionPartialUpdate
HTTP HEADER Accept: application/json
Method: PATCH
Request body:

{
	"foo": "bar",
	"nested": {
		"foo": "bar"	
	}
}

Params:

format = json
id = 123
associations = array(0)
data = {"foo": "bar", "nested": {"foo": "bar"}}
query = array(0)

Delete:

URL: /api/users/123\ApiModule\UsersPresenter::actionDelete
HTTP HEADER Accept: application/json
Method: DELETE
Request body: Empty
Params:

format = json
id = 123
associations = array(0)
data = ""
query = array(0)

Options:

For more about OPTIONS documentation see w3.org.

URL: /api/users\ApiModule\UsersPresenter::actionOptions
HTTP HEADER Accept: application/json
Method: OPTIONS
Request body: Empty
Params:

format = json
associations = array(0)
data = ""
query = array(0)

Associations:

Last item (pair) before . is main resource. Everything what is before the last item are associations (apigee.com).

URL: /api/users/1/comments\ApiModule\CommentsPresenter::actionReadAll|actionCreate|actionUpdate|actionDelete
HTTP HEADER Accept: application/json
Method: GET, POST, PUT, DELETE
Request body: Empty
Params:

format = json
associations = array(
	users => 1
)
data = ""
query = array(0)

URL: /api/users/123/comments/456\ApiModule\CommentsPresenter::actionRead|actionCreate|actionUpdate|actionDelete
HTTP HEADER Accept: application/json
Method: GET, POST, PUT, DELETE
Request body: Empty
Params:

format = json
id = 456
associations = array(
	users => 123
)
data = ""
query = array(0)

URL: /api/users/1/blogs/2/comments\ApiModule\CommentsPresenter::actionReadAll|actionCreate|actionUpdate|actionDelete
HTTP HEADER Accept: application/json
Method: GET, POST, PUT, DELETE
Request body: Empty
Params:

format = json
id = 1
associations = array(
	users => 1
	blogs => 2
)
data = ""
query = array(0)

URL versioning:

RestRoute provides you with option to version your API in URL. Each version is represented by separate module in your application.

First, you define regexp which is used to detect if the version parameter is present in URL. It must be in the begging of the path. Then, you define version to module mapping. NULL key stands for default module, if version parameter doesn't get detected.

$router[] = (new RestRoute('Api')) // Optional module
    ->useURLModuleVersioning(
        RestRoute::MODULE_VERSION_PATH_PREFIX_PATTERN,     // Regex for URL version
        [                  // URL fragment to module mapping
          NULL => 'V1',    // Default version module is V1
          'v1' => 'V1',    // /v1 to module V1
          'v2' => 'V2'     // /v2 to module V2
        ]
    );

RestRoute will now map your requests to presenters like this (URL -> Presenter):

/api/foo        ->  Api:V1:Foo
/api/v1/foo     ->  Api:V1:Foo
/api/v2/foo     ->  Api:V2:Foo

File uploads:

RestRoute reads standard PHP input and data puts to $data param in action. This is fit for one file upload or upload with chunks because it is a RAW data.

For multiple file upload RestRoute just set files when creates \Nette\Application\Request. In presenter just inject \Nette\Application\Request service and use these files.

class FooPresenter {
  /** @var \Nette\Application\Request @inject */
  public $request;

  public function actionCreate () {
    $files = $this->request->getFiles();
  }
}

Development

RestRoute is developed in Docker container via docker compose command.

Example:

$ docker compose run --rm default install  # install deps via composer
$ docker compose run --rm default  # runs tests in container

Attach to container:

$ docker compose run --rm default bash # runs bash in container and attach tty

adamstipak/nette-rest-route 适用场景与选型建议

adamstipak/nette-rest-route 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 152.58k 次下载、GitHub Stars 达 67, 最近一次更新时间为 2012 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 adamstipak/nette-rest-route 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 67
  • Watchers: 10
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-12-12