小蛋糕 / webservice
为 CakePHP 设计的简洁 Webservice
3.0.0
2021-09-11 08:18 UTC
Requires
- cakephp/orm: ^4.2
Requires (Dev)
- cakephp/cakephp: ^4.2
- cakephp/cakephp-codesniffer: ^4.0
- phpunit/phpunit: ^8.5 || ^9.3
README
将 CakePHP ORM 的强大功能带入您最喜欢的 Webservice。
安装
使用 Composer
composer require muffin/webservice
然后需要加载插件。您可以使用 shell 命令
bin/cake plugin load Muffin/Webservice
用法
数据源配置
在您的 app.php
中,在 Datasources
下添加一个新的 webservice
配置
'Datasources' => [ // Other db config here 'webservice' => [ 'className' => \Muffin\Webservice\Datasource\Connection::class, 'service' => 'Articles', // Any additional keys will be set as Driver's config. ], ],
如果您正在制作插件,则传统上数据源配置密钥名称应该是插件名称的下划线版本。
作为 ORM
创建驱动程序
<?php namespace App\Webservice\Driver; use Cake\Http\Client; use Muffin\Webservice\Driver\AbstractDriver; class Articles extends AbstractDriver { /** * Initialize is used to easily extend the constructor. */ public function initialize(): void { $this->setClient(new Client([ 'host' => 'example.com' ])); } }
创建一个 webservice
<?php namespace App\Webservice; use Muffin\Webservice\Datasource\Query; use Muffin\Webservice\Datasource\ResultSet; use Muffin\Webservice\Webservice\Webservice; class ArticlesWebservice extends Webservice { /** * Executes a query with the read action using the Cake HTTP Client */ protected function _executeReadQuery(Query $query, array $options = []) { $response = $this->getDriver()->getClient()->get('/articles.json'); if (!$response->isOk()) { return false; } $resources = $this->_transformResults($query->endpoint(), $response->json['articles']); return new ResultSet($resources, count($resources)); } }
创建一个端点(可选)
<?php namespace App\Model\Endpoint; use Muffin\Webservice\Model\Endpoint; class ArticlesEndpoint extends Endpoint { }
创建一个资源(可选)
<?php namespace App\Model\Resource; use Muffin\Webservice\Model\Resource; class Article extends Resource { }
使用它
<?php namespace App\Controller; use Muffin\Webservice\Model\EndpointLocator; class ArticlesController extends AppController { // Either set the default model type to "Endpoint" or explicitly specify // model type in loadModel() call as shown below. protected $_modelType = 'Endpoint'; public function index() { // This is required only if you haven't set `$_modelType` property to // "Endpoint" as shown above. $this->loadModel('Articles', 'Endpoint'); $articles = $this->Articles->find(); } }
作为驱动程序的基础
您还可以将此插件用作独立插件的基础,或用于管理自定义 webservice 驱动程序连接。
在官方文档编写之前,David Yell 写了一篇很好的帖子,帮助您入门。
Webservice 实现
作为 ORM
以下插件使用 Webservice ORM,让您轻松访问各种 Webservice
- GitHub 插件 - 提供对 GitHub REST API 的访问。
- NS 插件 - 提供对 NS (Nederlandse Spoorwegen) API 的访问。
- Stagemarkt 插件 - 提供对 SBB Stagemarkt REST API 的访问。
- Twitter 插件 - 提供对 Twitter REST 和流式 API 的访问。
作为驱动程序
以下插件实现了具有自己方法的 Webservice 驱动程序
补丁与功能
- 分支
- 修改、修复
- 测试 - 这很重要,所以不要不小心破坏了它
- 提交 - 不要修改许可证、todo、版本等。(如果您确实更改了任何内容,请将它们放入自己的提交中,这样我拉取时可以忽略它们)
- 拉取请求 - 主题分支的加分项
为了确保您的 PR 被考虑,您必须遵循 CakePHP 编码标准。
错误与反馈
http://github.com/usemuffin/webservice/issues
许可证
版权 (c) 2015-Present, [Use Muffin] 并在 MIT 许可证 下许可。