daniesy/rodels
Composer 安装命令:
composer require daniesy/rodels
包简介
Remote models for Laravel
README 文档
README
Remote models for Laravel.
Getting Started
Follow these instructions to get Rodels up and running in your Laravel project.
Prerequisites
You need Composer installed on your machine, but since you're already using Laravel, I guess you can skip this step.
Installing
Begin by installing this package through Composer.
composer require daniesy/rodels
Laravel Users
For Laravel users, there is a service provider you can make use of to automatically register the necessary bindings.
Laravel 5.5+ users: this step may be skipped, as we can auto-register the package with the framework.
// config/app.php 'providers' => [ '...', Daniesy\Rodels\RodelsServiceProvider::class ], 'aliases' => [ '...', 'Remote' => \Daniesy\Rodels\Facade\Remote::class, ],
When this provider is booted, you'll gain access to a helpful Remote facade, which you may use in your controllers.
public function index() { $users = Remote::users()->list(); return response()->json($users); }
In Laravel 5, of course add
use Remote;to the top of your controller.
Defaults
If using Laravel, there are only two configuration options that you'll need to worry about. First, publish the default configuration
php artisan vendor:publish
// Or...
php artisan vendor:publish --provider="Daniesy\Rodels\RodelsServiceProvider"
This will add a new configuration file to: config/rodels.php.
<?php return [ /* |-------------------------------------------------------------------------- | The remote API host |-------------------------------------------------------------------------- | | Set this value to the url of the remote API you want to connect to | */ 'host' => env('RODELS_HOST'), /* |-------------------------------------------------------------------------- | The HTTP client used to send HTTP requests |-------------------------------------------------------------------------- | | This option defines the http client that rodels will be using to connect | to the remote API. | | Supported: "curl" */ 'client' => 'curl', /* |-------------------------------------------------------------------------- | The authentication method |-------------------------------------------------------------------------- | | You can set the authentication method that will be used when connecting | to the API. | | Supported: "key" */ 'auth' => 'key', /* |-------------------------------------------------------------------------- | Authentication configuration |-------------------------------------------------------------------------- | | Configure the key authentication method. | */ 'key' => [ 'name' => 'api-key', 'value' => env('RODELS_KEY') ], ];
Using the library.
We add two artisan commands, one will create endpoints and the other one rodels.
Endpoints
The first step when using Rodels is to create an endpoint.
php artisan make:endpoint Users -r
This will create a new class in app\Endpoints- in this case Users.php.
In this class you should define all methods related to your endpoint.
All endpoints names should be nouns in plural form
Using the -r or --rodel option, automatically creates a rodel for this endpoint.
<?php namespace App\Endpoints; use App\Rodels\User; use Daniesy\Rodels\Api\Components\Endpoint; use Daniesy\Rodels\Api\Components\RodelCollection; use Daniesy\Rodels\Api\Exceptions\InvalidModelException; class Users extends Endpoint { /** * @param array $params * @return RodelCollection * @throws InvalidModelException */ public function list(array $params = []) : RodelCollection { $response = $this->authRequest()->get("users", $params); return new RodelCollection($response, User::class); } /** * @param string $name * @return User */ public function find(string $name) : User { $response = $this->authRequest()->get("users", compact('name')); return new User($response); } }
Rodels
For every endpoint, there should be a rodel created as well. Rodels are like Models, but instead of using them for databases, they're used for remote APIs.
Create a new rodel with this command:
php artisan make:rodel User
This will create a new rodel class in app\Rodels- in this case User.php.
All rodels names should be nouns in singular form.
daniesy/rodels 适用场景与选型建议
daniesy/rodels 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.44k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 daniesy/rodels 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daniesy/rodels 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2020-03-10