restyphp/resty
最新稳定版本:0.10.0
Composer 安装命令:
composer require restyphp/resty
包简介
Create beautifull REST APIs
README 文档
README
Deprecated
Resty
Librería super sencilla, basada en Slim, para crear servicios Restfull.
Versión estable
0.10.0
License
The MIT License (MIT). Ver el archivo LICENSE para más información
Documentación
(pendiente)
Ver archivo doc/README.md
Ejemplos
Devolver un mensaje JSON simple
$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();
Resultado
Como se puede ver, automáticamente devuelve la respuesta en formato json (ver cabeceras)
$ curl -H "Cache-Control: no-cache" -v "http://localhost: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
Devolver un mensaje JSON usando Hateoas
$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();
Respuesta
$ curl -H "Cache-Control: no-cache" -v "http://localhost: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
}
Ejemplo excepción
$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();
Respuesta
$ curl -H "Cache-Control: no-cache" -v "http://localhost:5005"
* Rebuilt URL to: http://localhost: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
Ejemplo ruta no encontrada
$ curl -H "Cache-Control: no-cache" -v "http://localhost: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:http:\/\/localhost:5005\/notfound",
"code":0,
"status":404
}
]
}
* Closing connection 0
restyphp/resty 适用场景与选型建议
restyphp/resty 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 105 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 07 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「rest」 「api」 「slim」 「restfull」 「JSON-API」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 restyphp/resty 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 restyphp/resty 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 restyphp/resty 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
High-performance, opinionated PHP 8 web framework with O(1) routing, parallel async MySQL, type-safe asset bridge, and React-island frontend
BlockCypher's PHP SDK for REST API
统计信息
- 总下载量: 105
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-28