mcuadros/silex-hateoas

此包已被弃用且不再维护。未建议替代包。
此包的最新版本(dev-master)未提供许可证信息。

dev-master 2013-06-12 18:28 UTC

This package is not auto-updated.

Last update: 2020-08-16 18:32:44 UTC


README

这是一个在Silex上构建RESTful HATEOAS服务的示例。

要求

  • PHP 5.3.x;
  • silex/silex
  • nocarrier/hal
  • shrikeh/teapot
  • popshack/silex-hal-serviceprovider

安装

推荐通过 composer 安装此示例。您可以在 Packagist 上查看 包信息。

git clone https://github.com/mcuadros/silex-hateoas silex-hateoas
cd silex-hateoas
git checkout basic
composer install

示例

您可以使用内置的PHP服务器运行示例项目

php -S 0.0.0.0:8080 index.php

检索所有资源

curl https://:8080/articles -X GET -i
HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.4.12
Cache-Control: no-cache
Date: Wed, 12 Jun 2013 13:57:22 GMT
Content-Type: application/hal+json

{
  "_links": {
    "self": {
      "href": "\/articles"
    },
    "search": {
      "href": "\/articles\/{order_id}"
    }
  },
  "_embedded": {
    "article": [
      {
        "id": 1,
        "title": "foo",
        "author": "bar",
        "votes": 121,
        "_links": {
          "self": {
            "href": "\/articles\/1"
          }
        }
      },
      {
        "id": 2,
        "title": "qux",
        "author": "baz",
        "votes": 423,
        "_links": {
          "self": {
            "href": "\/articles\/2"
          }
        }
      },
      {
        "id": 3,
        "title": "bar",
        "author": "qux",
        "votes": 23,
        "_links": {
          "self": {
            "href": "\/articles\/3"
          }
        }
      }
    ]
  }
}

检索一个资源

curl https://:8080/articles/1 -X GET -i
HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.4.12
Cache-Control: no-cache
Date: Wed, 12 Jun 2013 14:14:05 GMT
Content-Type: application/hal+json

{
  "id": 1,
  "title": "foo",
  "author": "bar",
  "votes": 121,
  "_links": {
    "self": {
      "href": "\/articles\/1"
    }
  }
}

创建资源

curl https://:8080/articles -X PUT -i -d "title=HATEOAS%20Restful%20Demo&author=Mximo%20Cuadros"
HTTP/1.1 201 Created
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.4.12
Cache-Control: no-cache
Date: Wed, 12 Jun 2013 14:00:08 GMT
Content-Type: application/hal+json

{
  "title": "HATEOAS Restful Demo",
  "author": "Mximo Cuadros",
  "_links": {
    "self": {
      "href": "\/articles\/4"
    }
  }
}

修改资源

curl https://:8080/articles/1 -X POST -i -d "author=Maximo%20Cuadros"
HTTP/1.1 201 Created
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.4.12
Cache-Control: no-cache
Date: Wed, 12 Jun 2013 14:00:08 GMT
Content-Type: application/hal+json

{
  "id": 1,
  "title": "foo",
  "author": "Maximo Cuadros",
  "votes": 121,
  "_links": {
    "self": {
      "href": "\/articles\/1"
    }
  }
}

删除资源

curl https://:8080/articles/1 -X DELETE -i"
HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.4.12
Cache-Control: no-cache
Date: Wed, 12 Jun 2013 14:04:53 GMT
Content-Type: application/hal+json

许可证

MIT,见 LICENSE