euskadi31 / rest-service-provider
A Rest Service Provider for Silex 2.0
v1.2.9
2015-10-21 16:58 UTC
Requires
- silex/silex: ~2.0@dev
- symfony/debug: ~2.7
Requires (Dev)
- leaphub/phpcs-symfony2-standard: ~2.0.1
- phpunit/phpunit: ^4.7
- satooshi/php-coveralls: dev-master
README
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.