nazarii-kretovych / laravel-api-model-driver
Laravel 7 Eloquent 模型的 API 驱动器
v1.0.3
2020-09-23 11:17 UTC
Requires
- php: ^7.2
- ext-curl: *
- illuminate/database: ^7.0
This package is auto-updated.
Last update: 2024-09-23 19:56:50 UTC
README
该库允许创建并将 API 连接分配给 Laravel 7 Eloquent 模型,并使用 Laravel 查询构建器构建查询字符串,以从数据库连接获取数据的方式获取数据。它还允许使用 Eloquent 关联。
一旦开发人员定义了新 API 连接的配置并使相关模型类使用该连接,他们就不需要考虑 API 调用、身份验证等问题。他们只需像处理具有 MySQL 连接的常规模型一样处理这些模型。然而,该库仅支持从 API 服务检索数据。
还可以配置自动时区转换,如果 API 服务以不同时区向客户端提供时间值,这样开发人员在编写代码时就不必考虑它。
功能
- 支持以下查询构建器函数:where、whereIn、whereBetween、orderBy 和 limit;
- 支持 Eloquent 关联;
- 自动将具有数组的查询参数名称复数化;
- 自动转换时间 JSON 属性和时间查询参数的时区;
- 可以定义多个 API 连接,每个连接都有自己的配置和身份验证;
- 自动拆分过长查询字符串(请参阅 max_url_length 参数);
- 该库使用 php-curl 扩展执行 API 调用,以便后续请求重用首次请求期间建立的连接,而无需浪费时间建立新连接。
安装
使用 composer 安装库
composer require nazarii-kretovych/laravel-api-model-driver
配置
打开 config/database.php 并添加新的 API 连接
<?php return [ // ... 'connections' => [ // ... 'example_com_api' => [ 'driver' => 'laravel_api_model_driver', 'database' => 'https://example.com/api', // You can define headers that will be sent to the API service in each request. // You might need to put your authentication token in a header. 'headers' => [ 'Authorization: Bearer TOKEN_HERE', ], // If the API service has Laravel Passport Client Credentials authentication, // you can define client ID and client secret here: 'auth' => [ 'type' => 'passport_client_credentials', 'url' => 'https://example.com/oauth/token', 'client_id' => 1, 'client_secret' => 'SECRET_HERE', ], // Define default query parameters. 'default_params' => [ 'per_page' => 1000, // this parameter is required 'del' => 'no', ], // If the generated URL is longer than **max_url_length**, // its query string will be split into several parts, and the data will be retrieved for each part separately. 'max_url_length' => 8000, // default: 4000 // The following configuration will generate the following query string // for ->whereIn('id', [1,3]): ids[]=1&ids[]=3 'pluralize_array_query_params' => true, // default: false 'pluralize_except' => ['meta'], // pluralization skips these query params // If the API service provides its clients with time values in a different time zone, // you can define the following configuration, which will enable automatic time zone conversion. 'timezone' => 'Europe/Kiev', 'datetime_keys' => ['created_at', 'updated_at', 'start_time', 'end_time'], ], ], ];
创建新的模型类并设置其连接
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model { protected $connection = 'example_com_api'; protected $table = 'articles'; // optional. Laravel generates it from the name of the class }
用法
<?php Article::with(['dbModel', 'apiModel.anotherApiModel']) ->where('status', 'published') ->whereIn('author_id', [1, 2]) ->whereBetween('publish_time', ['2020-08-01 00:00:00', '2020-08-04 23:59:59']) ->where('id', '>=', 3) ->where('id', '<=', 24); ->orderBy('publish_time', 'desc') ->limit(20) ->get(); // The library will generate the following URL for retrieving articles: // https://example.com/api/articles?status=published&author_ids[]=1&author_ids[]=2&min_publish_time=2020-08-01+00%3A00%3A00&max_publish_time=2020-08-04+23%3A59%3A59&min_id=3&max_id=24&order_by=publish_time&sort=desc&per_page=20