co-stack/api
Composer 安装命令:
composer require co-stack/api
包简介
TYPO3 REST API foundation. Built using an own middleware stack
README 文档
README
Powered by co-stack.com
About
This TYPO3 Extension adds a generic API capability to TYPO3. The strength of this extension lies in its integration in TYPO3 itself and its ease of being used by other extensions. If you want to create a fast and safe API in TYPO3 you should use this extension.
There is no purpose in installing this extension without another extension that requires it.
Features
- Based on its own extendable PSR-15 request Middleware stack.
- Early entrypoint: The API target is executed as soon as possible.
- Frontend entrypoint: The API target is executed after the TYPO3 frontend was booted. TypoScript etc. is available.
- Automatic response formatting into XML, JSON, CSV and more.
Usage
Creating an API endpoint with EXT:api is simple and fast. EXT:api and your extension must be installed for the following steps.
- Create your API endpoint class. It can be anywhere but it is good practice to put it in
Classes/Api. Name your class after its use case. (e.g.Vendor\Packaga\Api\VersionApi). Your API class must implement the interface\CoStack\Api\Api\Api. Return an array with some values to begin with. - Create your API registration file in
Configuration/Api.phpand register your API class. The file must return an associative array. The associative index is theROUTE, or API endpoint name and will be part of the URL. Here is an example with the StatusApi class.return [ 'status' => [ 'class' => \CoStack\Api\Api\StatusApi::class, ], ]; - Login as administrator to your TYPO3 Backend. Go to the root page (ID 0) and select the list module. Create a new API token. Your registered API class should show up in the API scope list. Give your token a name and add your API endpoint to the list of APIs in scope. Safe the token to generate the token's secret.
- You can test your API by using curl. Replace
SECRET,DOMAIN,EXTKEYandROUTEwith your values:\curl -H "Accept: application/json" -H "X-T3API-TOKEN: <SECRET>" https://<DOMAIN>/api/<EXTKEY>/<ROUTE>\ Example:curl -H "Accept: application/json" -H "X-T3API-TOKEN: a220d5a473232e636f845e0f6919981a1baf13e97758d83b2bde7c5ec68954b7" https://local.myproject.de/api/api/statusYou should see the array JSON encoded. (As of EXT:api 3.0 you can also use the GET parametertokeninstead of theX-T3API-TOKENheader)
Advanced usage
Automatic response formatting
Full documentation: ResponseFormatting.md
EXT:api tries to determine which content type was requested and formats the response accordingly. This spares you the part of the identification and formatting of the response.
If you request /api/api/status.xml you will receive XML as response, if you request /api/api/status.json, you will
get the response as JSON string. Calling the API without a file extension /api/api/status can still send an Accept
header, which contains a list of prioritized mime types which should be returned. (Your browser typically sends
something like text/html,application/xml;q=0.9,*/*;q=0.8). EXT:api will try to find a formatter for the highest
prioritized mime type to format the response. If no Accept header was given, the mime type configured for the API will
be used.

Additional middleware
Full documentation: Middlewares.md
The EXT:api middleware stack is fully configurable.
Register your own middleware in Configuration/RequestMiddlewares.php:
<?php
return [
'api' => [
'vendor/ext/request/logger' => [
'target' => \Vendor\Ext\Middleware\Api\Logger::class,
'before' => [
'co-stack/api/router',
],
],
],
];
API route configuration
Full documentation: ApiRoutes.md
Creating APIs is very easy. Create the file Configuration/Api.php in your extension and register your API.
<?php
declare(strict_types=1);
return [
'v1/extension/version' => [
'class' => \CoStack\Api\Api\ExtensionVersionApi::class,
],
];
Replace EXT_KEY with the extension key of your extension.
Call the API: https://host.tld/api/EXT_KEY/v1/extension/version
Debugging
To develop and debug your API calls you should set TYPO3's displayErrors to wither 1 for local development.
In public or production environments displayErrors should be -1 and your devIPmask should be set.
Enabling this setting will show exception instead of failing silently.
curl
Your API responses may be empty if something went wrong. You can add following options to your curl call to get more information:
-I: Display the response headers (especially useful if you do not see any response).-H "X-T3API-SAPI: cli": Force the exception to be rendered for CLI.
Following HTTP Status codes are currently implemented:
- 403: You did not include an authentication header (
X-T3API-TOKEN) or the provided secret is incorrect. - 406: You did not include an
acceptheader in your request or the accepted mime is not supported. Always try withapplication/jsonfirst. - 429: You hit the hard rate limit. Increase the requests per minute in the extension settings.
You can set this to
0to disable the rate limiter, but you must be aware of security implications (e.g. DoS). - 500: An exception occurred in your API class. Check the logs.
PhpStorm
When you do not get the results you expect from an API call you can debug requests with xDebug and PhpStorm.
Please use the internet search if you have any questions about xDebug or PhpStorm. We will not answer inquiries on these topics.
- Create a new .http scratch file. Replace
SECRET,DOMAIN,EXTKEYandROUTEwith your values):https://<DOMAIN>/api/<EXTKEY>/<ROUTE> Accept: application/json X-T3API-TOKEN: <SECRET> - Add a stop point in
\CoStack\Api\Middleware\Frontend\EarlyApiMiddleware::process. - Click on the green arrow left of the URL and select PHP Debug.
- If your project is set up correctly it should hit your stop point.
co-stack/api 适用场景与选型建议
co-stack/api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.16k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「middleware」 「token」 「typo3」 「eID」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 co-stack/api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 co-stack/api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 co-stack/api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
PSR-15 middleware to handle CSRF-token verification
统计信息
- 总下载量: 1.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-only
- 更新时间: 2020-04-22