An open API service indexing awesome lists of open source software.

https://github.com/euskadi31/restserviceprovider

A Rest Service Provider for Silex 2.0
https://github.com/euskadi31/restserviceprovider

Last synced: 3 months ago
JSON representation

A Rest Service Provider for Silex 2.0

Awesome Lists containing this project

README

        

# Silex Rest Service Provider

[![Build Status](https://img.shields.io/travis/euskadi31/RestServiceProvider/master.svg)](https://travis-ci.org/euskadi31/RestServiceProvider)
[![SensioLabs Insight](https://img.shields.io/sensiolabs/i/c04aee90-91be-4ea6-8f01-f0070806f1bb.svg)](https://insight.sensiolabs.com/projects/c04aee90-91be-4ea6-8f01-f0070806f1bb)
[![Coveralls](https://img.shields.io/coveralls/euskadi31/RestServiceProvider.svg)](https://coveralls.io/github/euskadi31/RestServiceProvider)
[![HHVM](https://img.shields.io/hhvm/euskadi31/RestServiceProvider.svg)](https://travis-ci.org/euskadi31/RestServiceProvider)
[![Packagist](https://img.shields.io/packagist/v/euskadi31/rest-service-provider.svg)](https://packagist.org/packages/euskadi31/rest-service-provider)

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
register(new \Euskadi31\Silex\Provider\RestServiceProvider);
```

### Field filter

```php
register(new \Euskadi31\Silex\Provider\RestServiceProvider);

$app->get('/users', function() {
return $this->json([
[
'id' => 1,
'username' => 'John',
'email' => '[email protected]',
'enabled' => true
],
[
'id' => 2,
'username' => 'Jean',
'email' => '[email protected]',
'enabled' => true
]
]);
});
```

Request:

```http
GET /users?fields=username
```

Response:

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

### Jsonp response

```php
register(new \Euskadi31\Silex\Provider\RestServiceProvider);

$app->get('/users', function() {
return $this->json([
[
'id' => 1,
'username' => 'John',
'email' => '[email protected]',
'enabled' => true
],
[
'id' => 2,
'username' => 'Jean',
'email' => '[email protected]',
'enabled' => true
]
]);
});
```

Request:

```http
GET /users?callback=Acme.process
```

Response:

```js
/**/Acme.process([
{
"id": 1,
"username": "John",
"email": "[email protected]",
"enabled": true
},
{
"id": 2,
"username": "Jean",
"email": "[email protected]",
"enabled": true
}
]);
```

### Pretty print response

```php
register(new \Euskadi31\Silex\Provider\RestServiceProvider);

$app->get('/users', function() {
return $this->json([
[
'id' => 1,
'username' => 'John',
'email' => '[email protected]',
'enabled' => true
],
[
'id' => 2,
'username' => 'Jean',
'email' => '[email protected]',
'enabled' => true
]
]);
});
```

Request:

```http
GET /users?pretty=0
```

Response:

```json
[{"id":1,"username":"John","email":"[email protected]","enabled":true},{"id":2,"username":"Jean","email":"[email protected]","enabled":true}]
```

### Error response

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

## License

RestServiceProvider is licensed under [the MIT license](LICENSE.md).