restyphp / resty
此包已被弃用且不再维护。未建议替代包。
创建美观的REST API
0.10.0
2017-06-28 02:43 UTC
Requires
- php: >=7.0.0
- restyphp/slim-json-handler-error: 0.4.*
- slim/slim: 3.8.*
Requires (Dev)
- phpunit/phpunit: 6.1.*
- squizlabs/php_codesniffer: 3.0.*
README
已弃用
Resty
一个非常简单的基于Slim的库,用于创建Restful服务。
稳定版本
0.10.0
许可证
MIT许可证(MIT)。查看LICENSE文件以获取更多信息
文档
(待定)
查看doc/README.md文件
示例
返回简单的JSON消息
$path = realpath(__DIR__.'/../');
require $path."/vendor/autoload.php";
// uses
use Resty\Api;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
// configuración
$configFilePath = __DIR__.'/config.php';
$config = include_once $configFilePath;
// Instancia la api
$app = new Api($config);
// Ruta
$app->get(
'/',
function (Request $request, Response $response) {
$data = ['status' => 'ok'];
return $response->write($data);
}
);
$app->run();
结果
如上图所示,它自动返回json格式的响应(查看头部信息)
$ curl -H "Cache-Control: no-cache" -v "https://:5005/"
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5005 (#0)
> GET / HTTP/1.1
> Host: localhost:5005
> User-Agent: curl/7.47.0
> Accept: */*
> Cache-Control: no-cache
>
< HTTP/1.1 200 OK
< Host: localhost:5005
< Connection: close
< X-Powered-By: PHP/7.0.12
< Content-Type: application/json;charset=utf-8
< Content-Length: 15
<
{
"status":"ok"
}
* Closing connection 0
使用Hateoas返回JSON消息
$path = realpath(__DIR__.'/../');
require $path."/vendor/autoload.php";
// uses
use Resty\Api;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Mostofreddy\Hateoas\Message;
// configuración
$configFilePath = __DIR__.'/config.php';
$config = include_once $configFilePath;
// Instancia la api
$app = new Api($config);
// Ruta
$app->get(
'/',
function (Request $request, Response $response) {
$usuario = new Message();
$usuario->add("Nombre", "Federico")
->addLink("self", "http://myapi/user/1");
$returnMessage = new Message();
$returnMessage->add("Reporte", "usuarios")
->add("usuarios", [$usuario])
->addLink("self", "http://myapi/usuarios");
return $res->ok($returnMessage, 200, JSON_PRETTY_PRINT);
}
);
$app->run();
响应
$ curl -H "Cache-Control: no-cache" -v "https://:5005"
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5005 (#0)
> GET /saludo HTTP/1.1
> Host: localhost:5005
> User-Agent: curl/7.47.0
> Accept: */*
> Cache-Control: no-cache
>
< HTTP/1.1 200 OK
< Host: localhost:5005
< Connection: close
< X-Powered-By: PHP/7.0.12
< Content-Type: application/json;charset=utf-8
< Content-Length: 260
<
{
"Reporte": "usuarios",
"usuarios": [
{
"Nombre": "Federico",
"links": {
"self": "http:\/\/myapi\/user\/1"
}
}
],
"links": {
"self": "http:\/\/myapi\/usuarios"
}
* Closing connection 0
}
异常示例
$path = realpath(__DIR__.'/../');
require $path."/vendor/autoload.php";
// uses
use Resty\Api;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Mostofreddy\Hateoas\Message;
// configuración
$configFilePath = __DIR__.'/config.php';
$config = include_once $configFilePath;
// Instancia la api
$app = new Api($config);
// Ruta
$app->get(
'/',
function (Request $request, Response $response) {
throw new \Exception("Algo paso");
}
);
$app->run();
响应
$ curl -H "Cache-Control: no-cache" -v "https://:5005"
* Rebuilt URL to: https://:5005/
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5005 (#0)
> GET / HTTP/1.1
> Host: localhost:5005
> User-Agent: curl/7.47.0
> Accept: */*
> Cache-Control: no-cache
>
< HTTP/1.1 500 Internal Server Error
< Host: localhost:5005
< Connection: close
< X-Powered-By: PHP/7.0.12
< Content-Type: application/json;charset=utf-8
< Content-Length: 134
<
{
"errors":[
{
"title":"Resty Application Error",
"details":[
{
"type":"Exception",
"code":0,
"message":"Algo paso"
}
],
"code":0,
"status":500
}
]
}
* Closing connection 0
找不到路由的示例
$ curl -H "Cache-Control: no-cache" -v "https://:5005/notfound"
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5005 (#0)
> GET /notfound HTTP/1.1
> Host: localhost:5005
> User-Agent: curl/7.47.0
> Accept: */*
> Cache-Control: no-cache
>
< HTTP/1.1 404 Not Found
< Host: localhost:5005
< Connection: close
< X-Powered-By: PHP/7.0.12
< Content-Type: application/json;charset=utf-8
< Content-Length: 122
<
{
"errors":[
{
"title":"Page not found",
"details":"Request => GET:https://:5005\/notfound",
"code":0,
"status":404
}
]
}
* Closing connection 0