euskadi31/rest-service-provider

A Rest Service Provider for Silex 2.0

v1.2.9 2015-10-21 16:58 UTC

This package is not auto-updated.

Last update: 2024-11-09 19:16:25 UTC


README

Build Status SensioLabs Insight Coveralls HHVM Packagist

Adding some REST capabilities to Silex 2.0, so you can more easily build RESTful APIs.

Install

Add euskadi31/rest-service-provider to your composer.json:

% php composer.phar require euskadi31/rest-service-provider:~1.2

Usage

Configuration

<?php

$app = new Silex\Application;

$app->register(new \Euskadi31\Silex\Provider\RestServiceProvider);

Field filter

<?php

$app = new Silex\Application;

$app->register(new \Euskadi31\Silex\Provider\RestServiceProvider);

$app->get('/users', function() {
    return $this->json([
        [
            'id'        => 1,
            'username'  => 'John',
            'email'     => 'john@example.com',
            'enabled'   => true
        ],
        [
            'id'        => 2,
            'username'  => 'Jean',
            'email'     => 'jean@example.com',
            'enabled'   => true
        ]
    ]);
});

Request:

GET /users?fields=username

Response:

[
    {
        "id": 1,
        "username": "John"
    },
    {
        "id": 2,
        "username": "Jean"
    }
]

Jsonp response

<?php

$app = new Silex\Application;

$app->register(new \Euskadi31\Silex\Provider\RestServiceProvider);

$app->get('/users', function() {
    return $this->json([
        [
            'id'        => 1,
            'username'  => 'John',
            'email'     => 'john@example.com',
            'enabled'   => true
        ],
        [
            'id'        => 2,
            'username'  => 'Jean',
            'email'     => 'jean@example.com',
            'enabled'   => true
        ]
    ]);
});

Request:

GET /users?callback=Acme.process

Response:

/**/Acme.process([
    {
        "id": 1,
        "username": "John",
        "email": "john@example.com",
        "enabled": true
    },
    {
        "id": 2,
        "username": "Jean",
        "email": "jean@example.com",
        "enabled": true
    }
]);

Pretty print response

<?php

$app = new Silex\Application;

$app->register(new \Euskadi31\Silex\Provider\RestServiceProvider);

$app->get('/users', function() {
    return $this->json([
        [
            'id'        => 1,
            'username'  => 'John',
            'email'     => 'john@example.com',
            'enabled'   => true
        ],
        [
            'id'        => 2,
            'username'  => 'Jean',
            'email'     => 'jean@example.com',
            'enabled'   => true
        ]
    ]);
});

Request:

GET /users?pretty=0

Response:

[{"id":1,"username":"John","email":"john@example.com","enabled":true},{"id":2,"username":"Jean","email":"jean@example.com","enabled":true}]

Error response

{
    "error": {
        "message": "No route found for \u0022GET \/me1\u0022",
        "type": "NotFoundHttpException",
        "code": 404
    }
}

License

RestServiceProvider is licensed under the MIT license.