diegonella / laravel-api-model-driver
Laravel Eloquent模型的API驱动程序
v1.1.1
2021-07-28 17:33 UTC
Requires
- php: ^7.2
- ext-curl: *
- illuminate/database: ~5.7.0
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 diegonella/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