freefri/cake-rest-api
Composer 安装命令:
composer require freefri/cake-rest-api
包简介
CakePHP Rest API
README 文档
README
Rest API for CakePHP 5.x
Configuration
-
Some configuration can be done using env variables (search for
env(in the project to find usages). -
Some configuration when working with plugins can be done from the main configuration (
config/app.phpfile).- Using the key
Swaggeris optional, but can be helpful to customize some directories (search forread('Swaggerfor details) - As sibling from the main
Appconfiguration use the namespace of the plugin followed by the wordPlugin.
- Using the key
For example, if your plugin namespace is called Example, create the following config file:
return [
'debug' => false,
'App' => [ ... ],
'Swagger' => [ ... ]
'ExamplePlugin' => [
'tablePrefix' => 'example_',
'routePath' => '/api/v1',
'routePath2' => '/api/v2',
],
];
The following configuration can be used:
tablePrefix: (optional) in case you want to add a prefix for you database tables, e.g.myplugin_users. Can be empty string.routePath: Definition for the beginning of the path for all routes in the pluginroutePath2: (optional) a second path prefix under which all the plugin routes are additionally registered, e.g. to expose the same routes under both/api/v1and/api/v2. When empty or not set, onlyroutePathis used.
Swagger
In order to make swagger UI with openapi description available, a new controller SwaggerJsonController must be
created, with the corresponding route definition. The method getContent can be overwritten in this controller
in order to add customization for the main spec info (title, description, version, etc.). Swagger generation can be
configured as described in the Configuration section above.
In any controller test the function $this->skipNextRequestInSwagger() can be used to do not add the next request.
Swagger configuration keys
All of the following live under the Swagger key in config/app.php (read via Configure::read('Swagger.*')). Every key is optional and falls back to a sensible default when not set.
readerClass: Fully qualified class name of the reader used to build the spec. Default:\RestApi\Lib\Swagger\FileReader\SwaggerReader.apiVersion: Version string shown underinfo.versionin the generated spec. Default: an auto-generated, date-based version (e.g.9.24.121530).jsonDir: Directory where the generated json spec files are stored and read from. Default:ROOT/<swaggerPath>/.fullFileDir: Directory where the single combined (full) json spec is written. Whennull, falls back to theSWAGGER_RELATIVE_FILE_DIRenv variable (appended toROOT); whenfalseor empty the full file is not generated; whentrue(or'1') the defaultjsonDiris used.fullFileName: File name for the combined full json spec. When empty, falls back to theSWAGGER_FULL_FILE_NAMEenv variable, then to the built-in default (SwaggerReader::FULL_SWAGGER_JSON).identifyEntities: Boolean. When truthy, entities expose their class name as a virtual field in the serialized output (used to identify entity types in the spec).displayErrorResponses: Boolean. When falsy (default), non-success responses (status codes outside 200–399) are removed from the spec; set truthy to keep error responses.displayMethodsWithoutOk: Boolean. When falsy (default), methods that have no success (200–399) response are removed from the spec; set truthy to keep them.addRoutePrefix: String prepended to every route path collected for the spec.acceptLanguage: Description (or value) for theAccept-Languageheader in generated requests. Whenfalsethe header is omitted; when empty a default descriptive string is used.
Swagger environment variables
Some Swagger behaviour is configured via environment variables (getenv) instead of config/app.php:
SWAGGER_NAMESPACE_TO_REMOVE(andSWAGGER_NAMESPACE_TO_REMOVE_1…SWAGGER_NAMESPACE_TO_REMOVE_20): Namespace prefixes to strip from generated type/schema names. When building a type name the entity class name hasModel\Entity\removed and every remaining\replaced withNs, so e.g.RestApi\Model\Entity\LogEntrybecomesRestApiNsLogEntry. SettingSWAGGER_NAMESPACE_TO_REMOVE=RestApistrips theRestApi\prefix first, yielding justLogEntry. Note this only removes leading namespace prefixes, e.g.SWAGGER_NAMESPACE_TO_REMOVE=CourseturnsCourseNsRanking(fromCourse\Ranking) intoRanking. Up to 21 values can be configured (the unsuffixed key plus suffixes_1through_20).SWAGGER_RELATIVE_FILE_DIR: Fallback directory for the combined full json spec, appended toROOT. Only used whenSwagger.fullFileDirisnull.SWAGGER_FULL_FILE_NAME: Fallback file name for the combined full json spec. Only used whenSwagger.fullFileNameis empty (before falling back to the built-inFULLswagger.json).
Logs in database
Logs in database can be easily enabled.
Just add to the Log section in config/app.php the className \RestApi\Lib\DatabaseLog::class and add a migration with phinx:
$this->table('log_entries', ['collation' => 'utf8mb4_general_ci', 'id' => false])
->addColumn('id', 'biginteger', [
'autoIncrement' => true,
'default' => null,
'limit' => null,
'null' => false,
'signed' => false,
])
->addPrimaryKey(['id'])
->addColumn('type', 'string', [
'default' => null,
'limit' => 50,
'null' => true,
])
->addColumn('title', 'string', [
'default' => null,
'limit' => 30,
'null' => true,
])
->addColumn('message', 'text', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('environment', 'string', [
'default' => null,
'limit' => 100,
'null' => true,
])
->addColumn('server', 'text', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('created', 'datetime', [
'default' => null,
'limit' => null,
'null' => true,
])
->addIndex(
[
'created',
]
)
->create();
License
The source code for the site is licensed under the MIT license, which you can find in the LICENSE file.
freefri/cake-rest-api 适用场景与选型建议
freefri/cake-rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.5k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「cakephp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 freefri/cake-rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 freefri/cake-rest-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 freefri/cake-rest-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Email Toolkit Plugin for CakePHP
Rhino, a CMS and Application-Framework plugin for CakePHP
A CakePHP plugin for sending mail via SparkPost's REST API
统计信息
- 总下载量: 7.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-13