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
- Host: GitHub
- URL: https://github.com/euskadi31/restserviceprovider
- Owner: euskadi31
- License: mit
- Created: 2015-06-19T09:48:47.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-21T17:08:01.000Z (over 9 years ago)
- Last Synced: 2025-01-19T18:49:06.145Z (5 months ago)
- Language: PHP
- Size: 203 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Silex Rest Service Provider
[](https://travis-ci.org/euskadi31/RestServiceProvider)
[](https://insight.sensiolabs.com/projects/c04aee90-91be-4ea6-8f01-f0070806f1bb)
[](https://coveralls.io/github/euskadi31/RestServiceProvider)
[](https://travis-ci.org/euskadi31/RestServiceProvider)
[](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).