klaasy1/restful

面向新手开发者的简单Restful库

dev-master 2016-07-19 07:12 UTC

This package is not auto-updated.

Last update: 2024-09-23 06:26:50 UTC


README

这是一个为REST新手提供的简单REST基类

安装

运行以下命令之一:

$ php composer.phar require klaasy1/restful: "dev-master"

或者

"klaasy1/restful": "dev-master"

将以下内容添加到您的 composer.json 文件的 require 部分中。

用法

use klaasy1\Restful\Restful;

$rest = new Restful();

//This part is your raw data
$myModel = new myModel();
$rawData = $myModel->getRawData();
       
echo $rest->getData($rawData);

默认返回JSON响应。您可以将Accept请求头设置为返回XML或HTML响应,请注意,您需要扩展AbtractRestful类并实现encodeHtml和encodeXml函数以满足您的响应数据需求,然后使用您的RestfulClass代替。

##示例

class RestfulClass extends AbtractRestful {

    //Use this as an example to implement this according to your need
    public function encodeHtml($responseData) {

        //Html respose data here
        return $htmlResponse;
    }

    //Use this as an example to implement this according to your need
    public function encodeXml($responseData) {
        //SimpleXMLElement Object
        $xml = new \SimpleXMLElement('<?xml version="1.0"?><root></root>');
        //Build your xml document here
        return $xml->asXML();
    }

}