torann/remote-model
Composer 安装命令:
composer require torann/remote-model
包简介
An eloquent-like model, for the Laravel framework.
README 文档
README
This model provides an eloquent-like base class that can be used to build custom models for remote APIs.
Installation
Install using composer:
$ composer require torann/remote-model
Clients
Custom request method
To implement a custom API request method in the model, simple extend the Torann\RemoteModel\Model class and use that extended model in the app models.
Example
<?php namespace App; use APIClient; use Torann\RemoteModel\Model; class BaseModel extends Model { /** * Make request through API. * * @return mixed */ protected function request($endpoint = null, $method, $params) { $endpoint = $endpoint ? $endpoint : $this->endpoint; $results = APIClient::$method($endpoint, $params); return $results ? $this->newInstance($results) : null; } }
Client Wrapper Method
$client = new Client();
$client->{ENDPOINT}()->{METHOD}();
ENDPOINT
The "snake case", plural name of the model class will be used as the endpoint name unless another name is explicitly specified. Using protected $endpoint = 'users'; at the top of the model, this is similar to the $table variable in Laravel models.
METHOD This is the action to take on the endpoint. It can be anything that the wrapper class provides.
Example Client Wrapper
<?php namespace PackageName\Api; use PackageName\Api\Exception\BadMethodCallException; use PackageName\Api\Exception\InvalidArgumentException; class Client { /** * The HTTP client instance used to communicate with API. * * @var HttpClient */ private $httpClient; /** * Instantiate a new client. */ public function __construct() { $this->httpClient = new HttpClient; } /** * @param string $name * * @throws InvalidArgumentException * * @return ApiInterface */ public function api($name) { switch ($name) { case 'users': $api = new Endpoints\Users($this); break; case 'reviews': $api = new Endpoints\Reviews($this); break; default: throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name)); } return $api; } /** * @param string $name * * @throws InvalidArgumentException * * @return ApiInterface */ public function __call($name, $args) { try { return $this->api($name); } catch (InvalidArgumentException $e) { throw new BadMethodCallException(sprintf('Undefined method called: "%s"', $name)); } } }
Example Endpoint for Client Wrapper
This is just to give an example.
<?php namespace PackageName\Api\Endpoints; use PackageName\Api\Client; class Users { /** * The client. * * @var \PackageName\Api\Client */ protected $client; /** * @param \PackageName\Api\Client $client */ public function __construct(Client $client) { $this->client = $client; } /** * Register a user. * * @param array $params * * @return array */ public function add(array $params) { return $this->client->post('users', $params); } /* * Update user data * * @param array $params * * @return object */ public function update(array $params) { return $this->client->patch('users/self', $params); } /** * Get extended information about a user by its id. * * @param string $user_id * * @return array */ public function find($user_id) { return $this->client->get('users/'.rawurlencode($user_id)); } }
Client Service Provider
An API client must be set before any data can be retrieved . To set the client use the static Model::setClient method.
Below is an example of the service provider way of setting the client.
<?php namespace App\Providers; use Torann\RemoteModel\Model; use PackageName\API\Client; use Illuminate\Support\ServiceProvider; class ApiServiceProvider extends ServiceProvider { /** * Bootstrap the application events. * * @return void */ public function boot() { Model::setClient($this->app['apiclient']); } /** * Register the service provider. * * @return void */ public function register() { $this->app->singleton('apiclient', function () { return new Client(); // API Client }); } /** * Get the services provided by the provider. * * @return string[] */ public function provides() { return [ 'apiclient' ]; } }
Example model
<?php namespace App; use DateTime; use Torann\RemoteModel\Model as BaseModel; class User extends BaseModel { protected $hidden = [ 'password' ]; protected $casts = [ 'age' => 'integer' ]; public function save() { return API::post('/items', $this->attributes); } public function setBirthdayAttribute($value) { $this->attributes['birthday'] = strtotime($value); } public function getBirthdayAttribute($value) { return new DateTime("@$value"); } public function getAgeAttribute($value) { return $this->birthday->diff(new DateTime('now'))->y; } }
Using model
$item = new User([ 'name' => 'john' ]); $item->password = 'bar'; echo $item; // {"name":"john"}
Change Log
0.1.0
- Fix parent ID bug
0.0.1
- First release
torann/remote-model 适用场景与选型建议
torann/remote-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.39k 次下载、GitHub Stars 达 48, 最近一次更新时间为 2015 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「model」 「laravel」 「eloquent」 「remote data」 「custom models」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 torann/remote-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 torann/remote-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 torann/remote-model 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
A PSR-7 compatible library for making CRUD API endpoints
Laravel 5 - Repositories to the database layer
This package provides an Address Model, Migration and Factory. It also provides the traits HasBillingAddres and HasPublicAddress
The YADM migrations
统计信息
- 总下载量: 14.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 48
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD
- 更新时间: 2015-09-14