symbiote/silverstripe-apiwrapper

提供围绕服务风格类的API包装器

安装次数: 4,415

依赖项: 3

建议者: 0

安全性: 0

星标: 2

关注者: 1

分支: 1

开放问题: 0

类型:silverstripe-vendormodule

1.6.0 2021-07-20 07:27 UTC

This package is auto-updated.

Last update: 2024-09-20 14:35:24 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License

将您的服务API包装在Web层中

Composer安装

composer require symbiote/silverstripe-apiwrapper:~1.0

需求

  • SilverStripe 4.1+

文档

快速入门

假设我们有一个类Symbiote\Watch\WatchService,我们希望通过/api/v1/watch/{methodname}将其暴露给Web请求

首先,实现webEnabledMethods,返回一个映射到可以触发它们的请求类型的数组,即

    public function webEnabledMethods()
    {
        return [
            'store' => 'POST',
            'list' => 'GET',
        ];
    }

在配置中;

---
Name: my_webservices
---
Symbiote\ApiWrapper\ApiWrapperController:
  versions:
    v1:
      watch: 'WatchServiceApiController' # The name of an injector service

SilverStripe\Core\Injector\Injector:
  WatchServiceApiController: # Referenced by above
    class: Symbiote\ApiWrapper\ServiceWrapperController
    properties:
      service: %$Symbiote\Watch\WatchService

附加选项

webEnabledMethods的返回可以提供其他信息,例如


return [
  'list' => [
    'type' => 'GET', 
    'call' => 'myMethod', // the name of a method on the service to execute 
    'public' => true,
    'match' => 'regex; see below',
    'perm' => 'CMS_Access_etc'
  ]
]

匹配

您可以在'match'键中定义一个正则表达式,它允许您匹配API和方法路径删除后URL子字符串中的参数。

注意,您必须使用命名捕获组定义正则表达式。

例如

'match' => '(?<parent>\d+)'

和URL

/api/v1/list/12

将调用'list'方法,将$parent参数设置为12。

请注意,这必须是完整的URL匹配,这意味着您应该为URL的所有部分定义捕获组。换句话说,'match'字符串被包装在"{^" . $match . "$}"中,这是实际执行的正则表达式。

基于令牌的认证

为用户添加用户令牌扩展,然后添加令牌认证中间件


SilverStripe\Security\Member:
  extensions:
    - Symbiote\ApiWrapper\TokenAccessible

SilverStripe\Core\Injector\Injector:
  SilverStripe\Security\AuthenticationHandler:
    properties:
      Handlers:
        token: '%$Symbiote\ApiWrapper\TokenAuthHandler'