承接 stonedz/pff2-rest 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

stonedz/pff2-rest

最新稳定版本:v4.1

Composer 安装命令:

composer require stonedz/pff2-rest

包简介

Creates REST controllers in pff2

README 文档

README

REST module for stonedz/pff2 (v4) to build JSON APIs with:

  • automatic REST method dispatch (GET/POST/PUT/DELETE)
  • optional auth hook per request
  • API version prefix routing (e.g. api1/...)
  • JSON exception output for API requests

Requirements

  • PHP + stonedz/pff2 v4
  • Module dependencies declared in this package:
    • exception_handler
    • pff2-permissions
  • Optional module:
    • pff2-annotations (only needed for annotation-based REST detection)

Installation

composer require stonedz/pff2-rest

Enable modules in app config

In your app config.user.php, ensure the modules list includes:

$pffConfig['modules'] = [
	'exception_handler',
	'pff2-permissions',
	'pff2-rest',
];

If you want annotation-based REST detection, also add:

'pff2-annotations',

Module configuration

Create (or override) config at:

app/config/modules/pff2-rest/module.conf.yaml

Example:

moduleConf:
  annotationName: Rest
  enableAuth: true
  authType: token
  authClass: ApiAuthCkecker
  apiVersions:
	- api1

Config keys

  • annotationName: method annotation name checked by pff2-annotations (when installed/enabled).
  • enableAuth: if true, auth class is invoked before handling request.
  • authType: currently informational in this module code.
  • authClass: class name under namespace \pff\models\ implementing IRestAuth.
  • apiVersions: URL prefixes treated as API version roots (api1, v2, etc).

How routing works

If request URL starts with a configured API version, pff2-rest rewrites route from:

/api1/users/42

to controller/action form:

Api1_Users/index/42

Then, if controller implements IRestController, action is replaced by HTTP method:

  • GETgetHandler
  • POSTpostHandler
  • PUTputHandler
  • DELETEdeleteHandler
  • OPTIONS → immediate 200

If pff2-annotations is installed and enabled, methods marked with the configured annotation name are also treated as REST requests.

Creating a REST controller

Implement pff\modules\Iface\IRestController:

<?php

namespace pff\controllers;

use pff\Abs\AController;
use pff\modules\Iface\IRestController;

class Api1_Users_Controller extends AController implements IRestController
{
	public function getHandler()
	{
		echo json_encode(['ok' => true, 'method' => 'GET']);
	}

	public function postHandler()
	{
		echo json_encode(['ok' => true, 'method' => 'POST']);
	}

	public function putHandler()
	{
		echo json_encode(['ok' => true, 'method' => 'PUT']);
	}

	public function deleteHandler()
	{
		echo json_encode(['ok' => true, 'method' => 'DELETE']);
	}
}

Auth integration

When enableAuth: true, module creates:

\pff\models\<authClass>

and calls:

authorize(IController $controller)

So your auth model should implement pff\modules\Iface\IRestAuth.

Example:

<?php

namespace pff\models;

use pff\Iface\IController;
use pff\modules\Iface\IRestAuth;
use pff\modules\Exception\RestException;

class ApiAuthCkecker implements IRestAuth
{
	public function authorize(IController $controller)
	{
		$token = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
		if ($token === '') {
			throw new RestException('Unauthorized', 401);
		}
	}
}

Error output format

For REST requests, uncaught exceptions are rendered as JSON:

{
  "error": true,
  "message": "...",
  "file": "...::..."
}

HTTP status code is taken from exception code.

Notes

  • The default sample value is ApiAuthCkecker (spelling from legacy config). Use any class name you want, as long as it exists under \pff\models\ and matches authClass.
  • This module sets JSON output automatically for matched REST requests.
  • For non-REST requests, normal pff2 flow is unchanged.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固