1. Go to this page and download the library: Download zoiloreyes/trest library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zoiloreyes / trest example snippets
/**
* 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()
)));
$sandwich = Sandwich::findOne($id); // GET http://pixelpt-sandwich-api.herokuapp.com/sandwich/$id
$sandwich->save(); // PUT http://pixelpt-sandwich-api.herokuapp.com/sandwich/$id (title=Double bacon cheese, price=9000)
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'
)
);
}
}