lokhman/silex-restful

Silex 2.0+ RESTful 中间件服务提供者

2.0.2 2017-04-19 12:43 UTC

This package is auto-updated.

Last update: 2024-09-16 21:41:31 UTC


README

StyleCI

Silex 2.0+ 微型框架提供的 RESTful 中间件服务提供者。

该项目是 silex-tools 库的一部分。

安装

您可以使用 Composer 安装 silex-restful

composer require lokhman/silex-restful

文档

注册 RestfulServiceProvider 可以轻松扩展您的应用程序路由,使用 JSON 请求/响应方法、错误处理和 JSON 参数接受。它的工作方式与 Silex 路由绑定(getpost 等)相同,支持自定义 controllers_factorymount 功能。

use Lokhman\Silex\Provider\RestfulServiceProvider;

$app->register(new RestfulServiceProvider());

// response is transformed to JSON string
$app['restful']->get('/api', function() {
    return ['version' => '1.0'];
});

// can mount controller collections
$app['restful']->mount('/api/v2', function($api) {
    // accepts parameters from "application/json" body
    $api->post('/submit', function(Request $request) {
        return ['params' => $request->request->all()];
    });
});

// can mount controller providers
class ApiBundle implements ControllerProviderInterface {

    function connect(Application $app) {
        $factory = $app['restful.controllers_factory'];

        $factory->get('/', function() {
            // will modify all exceptions to JSON compatible responses
            throw new GoneHttpException('API v3 is not supported anymore.');
        });

        return $factory->getControllerCollection();
    }

}

$app->mount('/api/v3', new ApiBundle());

许可证

库在 MIT 许可证下可用。包含的 LICENSE 文件详细描述了这一点。