lokhman/silex-restful

Silex 2.0+ service provider for RESTful middleware

2.0.2 2017-04-19 12:43 UTC

This package is auto-updated.

Last update: 2024-04-16 20:35:34 UTC


README

StyleCI

RESTful middleware service provider for Silex 2.0+ micro-framework.

This project is a part of silex-tools library.

Installation

You can install silex-restful with Composer:

composer require lokhman/silex-restful

Documentation

Registering RestfulServiceProvider will easily extend your application routing with JSON request/response methods, error handing and JSON parameter acceptance. It works in the same way as Silex routing binding (get, post, etc), supports own controllers_factory and mount functionality.

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());

License

Library is available under the MIT license. The included LICENSE file describes this in detail.