causal/simple_api
Composer 安装命令:
composer require causal/simple_api
包简介
Service to route HTTP/REST requests to your own TYPO3 controllers.
关键字:
README 文档
README
Service to route HTTP/REST requests to your own controllers.
BEWARE: This extension is expected to be used by TYPO3 developers and acts as a central hub to route requests to your own business logic.
Features
- Support for authenticated method calls
- Support for localized calls (taking the language into account)
- Support for cached calls and transparent access to the TYPO3 caching framework within your API handler
- Support for gzip payload if header
HTTP_ACCEPT_ENCODINGis present and containsgzip - Support for dynamically generating a documentation of your API
- Automatic flushing of cache entries when editing records in Backend (use
<table-name>%<uid>in the list of corresponding tags)
In addition, this supports dependency injection for your API handlers, you just need to use @inject or methods
prefixed by inject, as well-known when programming with Extbase.
Difference with EXT:routing
Unlike EXT:routing, this extension does not force you to map Extbase
controller/actions to route segments but instead basically lets you register a "segment" (typically the first one) and
then will simply route the whole request to a handle() method within your controller.
Registration of handlers
First of all you should add a dependency within your ext_emconf.php configuration file, either as a real constraint,
or as a suggestion, depending on what you prefer:
$EM_CONF[$_EXTKEY] = [
// snip
'constraints' => [
'depends' => [
'php' => '5.5.0-7.1.99',
'typo3' => '7.6.0-8.99.99',
],
'conflicts' => [],
'suggests' => [
'simple_api' => '',
],
],
];
Then, within ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['simple_api']['apiHandlers'][] = [
'route' => '/some-route',
'class' => \VENDOR\YourExt\Api\YourClass::class,
];
you may register a pattern instead of a fixed route:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['simple_api']['apiPatternHandlers'][] = [
'route' => '/members/\d+/history',
'class' => \VENDOR\YourExt\Api\YourClass::class,
];
Your handler must extend \Causal\SimpleApi\Controller\AbstractHandler.
Handler Keys
The registration array supports various keys:
-
route [mandatory]: The route to register.
-
class [mandatory]: The class handling requests to the corresponding route.
-
contentType [optional]: Content type of the payload accepted by a POST request, will decode it automatically before invoking your handler.
-
methods [optional]: The comma-separated list of HTTP methods accepted by the handler (e.g., "POST"). Defaults to no restrictions.
-
restricted [optional]: Whether the API call expects an authenticated call (using
HTTP_X_AUTHORIZATIONheader). If restricting access to part of your API, you must register a route with name/authenticatewhich will get theHTTP_X_AUTHORIZATIONheader, do something with it and return an array with following keys:success => true(orfalse). Will be passed as_authenticatedboolean flag to the API handler- Custom keys will be prefixed by
_and passed as-is to the API handler (e.g.,userwill become_user) - Custom boolean flag
demomay be used to specify that the authentication succeeded but with "demonstration" capabilities. This needs to be then handled in your API controller by checking, as expected, boolean flag parameter_demo.
Hint: If
HTTP_X_AUTHORIZATIONheader is present, the authentication will take place and your handler will be invoked regardless of the outcome of the call, if you did not explicitely marked your handler as "restricted". -
deprecated [optional]: Boolean flag to mark the corresponding route as deprecated in the documentation.
Payload
Following rules apply with the payload you return from your API handler:
- Payload is expected to be an array and will be returned as content-type
application/json. If you want to return another content-type (such as an image), you should do it in your own API handler andexit()afterwards. - If an exception is thrown, it is catched and encapsulated into a HTTP 500 error. The only exception is if exception
\Causal\SimpleApi\Exception\ForbiddenExceptionis thrown, it will throw a HTTP 403 error instead. - If no handlers are found, a HTTP 404 error is returned.
Known Issues and Workaround
-
Extbase repositories may be used within your API handlers but you need to manually invoke method
includeTCA()from the base class in order for the Extbase mapping to be available to returned objects. This call should typically be part of theinitialize()method you can override in your handler and which is called before invokinghandle().BEWARE: If you don't properly include the TCA of your domain model, you may corrupt the Extbase datamap cache by storing incomplete mapping definition into the cache backend for
extbase_datamapfactory_datamap(e.g., tablecf_extbase_datamapfactory_datamapwith out-of-the-box TYPO3 settings). In such case, you will need to flush that cache manually.
Installation
-
Clone this repository into
typo3conf/ext/simple_api:cd /path/to/typo3conf/ext/ git clone https://github.com/xperseguers/t3ext-simple_api.git simple_apiAlternatively, you may load it via composer:
composer require causal/simple_api -
Go to Extension Manager and activate extension
simple_api -
Add a rewrite rule to your
.htaccess. E.g.,RewriteRule ^api/(.*)$ /index.php?eID=simple_api&route=$1 [QSA,L]or, if you are using Nginx:
rewrite ^/api/(.*)$ /index.php?eID=simple_api&route=$1 last;This will have the effect of using this extension for handling requests starting with
api/.
Hint: If you need to support localization (&L=<some-language-uid>), then you should change the suggesting routing
above to include the root page uid of your website (&id=<some-uid>). This is needed because localization mode and
default language may differ in complex environments and thus cannot be inferred.
causal/simple_api 适用场景与选型建议
causal/simple_api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 600 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「TYPO3 CMS」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 causal/simple_api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 causal/simple_api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 causal/simple_api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
GraphQL authentication for your headless Craft CMS applications.
Set Links with a specific language parameter
Supercharged text field validation.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
A PSR-7 compatible library for making CRUD API endpoints
An interactive tour through the TYPO3 backend.
统计信息
- 总下载量: 600
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2017-04-19