diegonella/laravel-api-model-driver 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

diegonella/laravel-api-model-driver

Composer 安装命令:

composer require diegonella/laravel-api-model-driver

包简介

API driver for Laravel Eloquent models

README 文档

README

The library allows to create and assign an API connection to Laravel 7 Eloquent models and use Laravel query builder to build a query string and get data as if you get data from a database connection. It also allows to use Eloquent relationships.

Once developers define the configuration of a new API connection and make the related model classes use that connection, they don't need to think about API calls, authentication, etc. They just work with those models as if they are regular models that have a MySQL connection. However, the library only supports retrieving data from an API service.

There is also a possibility to configure automatic time zone conversion in case the API service provides the clients with time values in a different time zone so that the developers don't need to think about it while they are writing code.

Features

  • Support the following query builder functions: where, whereIn, whereBetween, orderBy and limit;
  • Support Eloquent relationships;
  • Automatic pluralization of the name of a query parameter that has an array;
  • Automatic time zone conversion for time JSON properties and time query parameters;
  • Possibility to define multiple API connections with their own configuration and authentication;
  • Automatic splitting query strings whose length is too long (see the max_url_length parameter);
  • The library makes API calls using the php-curl extension so that the subsequent requests reuse the connection that was established during the first request without wasting time on establishing new connections.

Installation

Install the library using composer:

composer require diegonella/laravel-api-model-driver

Configuration

Open config/database.php and add a new API connection:

<?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'],
        ],
    ],
];

Create a new model class and set its connection:

<?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
}

Usage

<?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

diegonella/laravel-api-model-driver 适用场景与选型建议

diegonella/laravel-api-model-driver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「laravel」 「eloquent」 「database driver」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 diegonella/laravel-api-model-driver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 diegonella/laravel-api-model-driver 我们能提供哪些服务?
定制开发 / 二次开发

基于 diegonella/laravel-api-model-driver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-28