Trabla: Luracast Restler & php: add support of custom format
Solving:
In this tutorial we will add new format - Image format into Luracast Restler
- great framework for creating api.
1. Create file "ImageFormat.php" and put it into
.../vendor/restler/framework/Luracast/Restler/Format folder
e.g. on my PC:
C:/xampp/htdocs/api/vendor/restler/framework/Luracast/Restler/Format
2. Open file "ImageFormat.php" and paste code below + save:
<?php
namespace Luracast\Restler\Format;
/**
* Description of ImageFormat
*
* @author Samurai Kit
*/
class ImageFormat extends Format{
public function decode($data) {
return $data;
}
public function encode($data, $humanReadable = false) {
return $data;
}
const MIME = 'image/*';
}
3. Find file Scope.php and add followind code
( File is located .../vendor/restler/framework/Luracast/Restler/ directory )
<?php
namespace Luracast\Restler;
/**
* Scope resolution class, manages instantiation and acts as a dependency
* injection container
*
* @category Framework
* @package Restler
* @author R.Arul Kumaran <arul@luracast.com>
* @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
* @version 3.0.0rc6
*/
class Scope
{
public static $classAliases = array(
//Core
'Restler' => 'Luracast\Restler\Restler',
//Format classes
'AmfFormat' => 'Luracast\Restler\Format\AmfFormat',
'JsFormat' => 'Luracast\Restler\Format\JsFormat',
'JsonFormat' => 'Luracast\Restler\Format\JsonFormat',
'HtmlFormat' => 'Luracast\Restler\Format\HtmlFormat',
'PlistFormat' => 'Luracast\Restler\Format\PlistFormat',
'UploadFormat' => 'Luracast\Restler\Format\UploadFormat',
'UrlEncodedFormat' => 'Luracast\Restler\Format\UrlEncodedFormat',
'XmlFormat' => 'Luracast\Restler\Format\XmlFormat',
'YamlFormat' => 'Luracast\Restler\Format\YamlFormat',
'CsvFormat' => 'Luracast\Restler\Format\CsvFormat',
'TsvFormat' => 'Luracast\Restler\Format\TsvFormat',
//Custom Format
'ImageFormat' => 'Luracast\Restler\Format\ImageFormat',
//Filter classes
'RateLimit' => 'Luracast\Restler\Filter\RateLimit',
//UI classes
'Forms' => 'Luracast\Restler\UI\Forms',
'Nav' => 'Luracast\Restler\UI\Nav',
'Emmet' => 'Luracast\Restler\UI\Emmet',
'T' => 'Luracast\Restler\UI\Tags',
//API classes
'Resources' => 'Luracast\Restler\Resources',
'Explorer' => 'Luracast\Restler\Explorer',
//Cache classes
'HumanReadableCache' => 'Luracast\Restler\HumanReadableCache',
'ApcCache' => 'Luracast\Restler\ApcCache',
'MemcacheCache' => 'Luracast\Restler\MemcacheCache',
3. Add new supported format in your index.php file
Example:
C:/xampp/htdocs/api/index.php
<?php
date_default_timezone_set('UTC');
require_once __DIR__ .'/vendor/autoload.php';
use Luracast\Restler\Restler;
use Luracast\Restler\Defaults;
$production_mode = false;
$r = new Restler( $production_mode );
$r->addAPIClass('Log');
$r->addAPIClass('Login');
$r->addAPIClass('Explorer');
$r->setSupportedFormats(
'JsonFormat'
, 'ImageFormat'
);
$r->handle();
No comments:
Post a Comment