rickwest/laravel-sportspress-api

用于SportsPress REST API(v2)的Laravel只读客户端


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub Static Analysis Status Total Downloads MIT Licensed

一个不野心勃勃的用于SportsPress REST API(v2)的只读客户端。此包基于我的 Laravel WordPress API包 构建,并提供了一种表达性强、流畅、Laravel风格的查询SportsPress API的方式。

// Without the package 👎
Http::get('https://example.com/wp-json/sportspress/v2/players', [
    'search' => 'lebron',
    '_embed' => 1,
    'orderby' => 'date',
    'order' => 'desc'
    '_fields' => 'title',
]);


// Using the package 👌
SportsPress::players()
    ->search('lebron')
    ->embed()
    ->latest()
    ->get('title');

除了流畅的查询构建器之外,您还可以受益于格式良好的响应,包括分页信息。

// Without the package 👎
$response = Http::get('https://example.com/wp-json/sportspress/v2/players');
$data = $response->json();
$pages = $response->header('X-WP-TotalPages');
$total = $response->header('X-WP-Total');


// Using the package 👌
$players = SportsPress::players()->get();

// $posts
[
    'data' => [...],
    'meta' => [
        'pages' => 1,
        'total' => 10,
    ],
],

安装

您可以通过Composer安装此包

composer require rickwest/laravel-sportspress-api

然后您需要将您的WordPress URL添加到您的 .env 文件中

WORDPRESS_URL=https://example.com

使用

此包将一个单例绑定到Laravel服务容器中,因此您可以直接从容器或通过依赖注入轻松解决WordPress客户端。或者,如果您更喜欢更短、更简洁的表达方式,此包还公开了 Facade 和辅助函数。

此包支持以下SportsPress资源:日历、事件、联赛、球员、位置、角色、赛季、员工、队伍、场馆

// Resolve service directly from container and access the Players API
app(SportsPress::class)->players();

// Resolve via Facade and access the Players API
SportsPress::players(); 

// Resolve service via helper and access the Players API
sportspress()->players();

// Supported resources
SportsPress::calendars() // Access the Calendars API
SportsPress::events() // Access the Events API
SportsPress::leagues() // Access the Leagues API
SportsPress::players() // Access the Players API
SportsPress::postitions() // Access the Positions API
SportsPress::roles() // Access the Roles API
SportsPress::seasons() // Access the Seasons API
SportsPress::staff() // Access the Staff API
SportsPress::teams() // Access the Teams API
SportsPress::venues() // Access the Venues API

// You can also access resources as properties
sportspress()->players

检索资源

通过调用资源类的 find 方法来通过ID获取单个资源:

SportsPress::players()->find(1);

有关更多示例、详细用法和完整选项列表,请参阅 https://github.com/rickwest/laravel-wordpress-api#retrieve-a-single-resource

检索资源集合

通过在资源上调用 get 方法来检索资源集合。您可以使用各种参数控制并过滤收到的响应,https://developer.wordpress.org/rest-api/reference/。此包提供了一些流畅的构建器方法,以便轻松、简洁地构建所需的查询。集合响应将被格式化,并包括有用的分页信息。

SportsPress::players()->get();

有关更多示例、详细用法和完整选项列表,请参阅 https://github.com/rickwest/laravel-wordpress-api#creating-updating-and-deleting-resources

创建、更新和删除资源

虽然此包主要用于从SportsPress API读取数据,但您可以使用资源类上的 send 方法执行写入操作。

SportsPress::players()->send(string $method, int $id, array $options);

有关更多示例、详细用法和完整选项列表,请参阅 https://github.com/rickwest/laravel-wordpress-api#retrieve-a-collection-of-resources

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请参阅我们关于如何报告安全漏洞的 安全策略

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件