markmercedes / trest
TRest是一个ORM,它将REST资源映射到PHP对象。设计用于需要从RESTful API消费数据的应用程序。
v2.1.2
2016-04-28 15:25 UTC
This package is not auto-updated.
Last update: 2024-09-24 06:31:38 UTC
README
TRest是一个ORM,它将REST资源映射到PHP对象。设计用于需要从RESTful API消费数据的应用程序。
用法
存在一个名为trest_init.php的示例文件,其中包含默认配置,可能有助于您了解如何在开始使用之前初始化库。
<?php /** * You should allways define the constant `TREST_DEFAULT_CACHE_TTL`, this constant the default time that items will be * cached in seconds, if you don't wanna catch your request define it and provide a value of 0/ */ define('TREST_DEFAULT_CACHE_TTL', 120); /** * The config named 'default' will be used for the models without the property * => protected static $configName = 'StackOverflow'; */ ConfigFactory::add('default', new Config(array( 'apiUrl' => 'http://pixelpt-sandwich-api.herokuapp.com/', 'singleItemNode' => 'sandwich', /** * Here you can provide a cache adapter to your connection. * * To create your own cache adapters, implement the interface TRest\Cache\CacheAdapterInterface * and provide an instance of your class to the configuration key named cacheAdapter of your connection. * * Here should be passed an instance of a class that implements the interface TRest\Cache\CacheAdapterInterface. * */ 'cacheAdapter' => new ClassImplementingCacheAdapterInterface() )));
简介
基本概念很简单,您有一个REST服务(http://pixelpt-sandwich-api.herokuapp.com/),您希望通过简单的ActiveRecord样式接口与之交互。
首先我们可以获取三明治
$sandwich = Sandwich::findOne($id); // GET http://pixelpt-sandwich-api.herokuapp.com/sandwich/$id
现在我们可以改变那个三明治的一些属性
$sandwich->name = 'Double bacon cheese'; $sandwich->price = 9000;
一旦我们完成,我们只需保存它,就会发出适当的REST调用
$sandwich->save(); // PUT http://pixelpt-sandwich-api.herokuapp.com/sandwich/$id (title=Double bacon cheese, price=9000)
模型
REST客户端是用于处理REST服务的ActiveRecord样式实现。您需要做的只是定义一些PHP类,这些类映射到Web上的某些REST服务。以下是一个示例,我们将三明治映射到http://pixelpt-sandwich-api.herokuapp.com/sandwich
<?php namespace Entities; use TRest\Models\Model; class Sandwich extends Model { /** * The name of the resource */ protected static $resource = 'sandwiches'; /** * In the case of this particular API, a single item in returned inside a node named with the resource * name singularized */ protected static $singleItemNode = 'sandwich'; /** * In the case of this particular API, a list of items in returned inside a node named with the resource * name pluralized */ protected static $listItemNode = 'sandwiches'; /* * Field definition */ public function fields() { return array( 'id' => array( 'type' => 'integer' ), 'title' => array( 'type' => 'string' ), 'price' => array( 'type' => 'integer' ) ); } /** * Retlations */ public function relations() { return array( 'ingredients' => array( 'class' => 'Ingredient', 'type' => self::HAS_MANY, 'postOnSave' => true, 'postSuffix' => '_attributes' ) ); } } class Ingredient extends Model{ protected static $resource = 'ingredients'; protected static $singleItemNode = 'ingredient'; protected static $listItemNode = 'ingredients'; public function relations() { return array( 'sandwich' => array( 'class' => 'Sandwich', 'type' => self::BELONGS_TO ) ); } public function fields() { return array( 'id' => array( 'type' => 'integer' ), 'name' => array( 'type' => 'string' ), 'quantity' => array( 'type' => 'integer' ) ); } }
现在当我们执行一些操作时,它将生成适当的REST请求,执行它,转换响应,并将结果填充到您的PHP对象中。
$sandwich = new Sandwich(); $sandwich->title = "mark"; $sandwich->price = 9200; $sandwich->ingredients = [ new Ingredient( (object)['name' => 'Bacon', 'quantity' => 2] ), new Ingredient( (object)['name' => 'Cheese', 'quantity' => 1] ) ]; $sandwich->save(); /** * POST http://pixelpt-sandwich-api.herokuapp.com/ * Results {"id":154,"title":"mark","price":9200, * "ingredients":[{"id":315,"name":"Bacon","quantity":2},{"id":316,"name":"Cheese","quantity":1}]} */
现在您可以继续使用模型并对其进行更多更改,从那时起,您将能够检索模型的id
$sandwich->id; // => 154 $sandwich->ingrediends[0]->id; // => 315
现在我们可以再次检索那个三明治
$sandwich = Sandwich::findOne($sandwich->id); // GET http://pixelpt-sandwich-api.herokuapp.com/154
或者您可以检索所有三明治对象
$sandwiches = Sandwich::find()-all();