Skip to content

Commit

Permalink
adding CORS capabilities and accept JSON req
Browse files Browse the repository at this point in the history
  • Loading branch information
vesparny committed Nov 28, 2013
1 parent e0443be commit b2687e1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Silex Simple REST
[![Latest Stable Version](https://poser.pugx.org/vesparny/silex-simple-rest/v/stable.png)](https://packagist.org/packages/nesbot/carbon) [![Total Downloads](https://poser.pugx.org/vesparny/silex-simple-rest/downloads.png)](https://packagist.org/packages/nesbot/carbon) [![Build Status](https://secure.travis-ci.org/vesparny/silex-simple-rest.png)](http://travis-ci.org/vesparny/silex-simple-rest) [![Dependency Status](https://www.versioneye.com/user/projects/52925eba632bac8d4d0000c1/badge.png)](https://www.versioneye.com/user/projects/52925eba632bac8d4d0000c1)
[![Latest Stable Version](https://poser.pugx.org/vesparny/silex-simple-rest/v/stable.png)](https://packagist.org/packages/vesparny/silex-simple-rest) [![Total Downloads](https://poser.pugx.org/vesparny/silex-simple-rest/downloads.png)](https://packagist.org/packages/vesparny/silex-simple-rest) [![Build Status](https://secure.travis-ci.org/vesparny/silex-simple-rest.png)](http://travis-ci.org/vesparny/silex-simple-rest) [![Dependency Status](https://www.versioneye.com/user/projects/52925eba632bac8d4d0000c1/badge.png)](https://www.versioneye.com/user/projects/52925eba632bac8d4d0000c1)


A simple silex skeleton application for writing RESTful API. Developed and maintained by [Alessandro Arnodo](http://alessandro.arnodo.net).
Expand All @@ -17,6 +17,10 @@ You need at least php **5.4.*** with **SQLite extension** enabled and **Composer
composer install
sqlite3 app.db < resources/sql/schema.sql
php -S 0:9001 -t web/

You can install the project also as a composer project

composer create-project vesparny/silex-simple-rest

Your api is now available at http://localhost:9001/api/v1.

Expand All @@ -32,10 +36,27 @@ From the root folder run the following command to run tests.
The api will respond to

GET -> http://localhost:9001/api/v1/notes
POST -> http://localhost:9001/api/v1/notes/{id}
POST -> http://localhost:9001/api/v1/notes
POST -> http://localhost:9001/api/v1/notes/{id}
DELETE -> http://localhost:9001/api/v1/notes/{id}

Your request should have 'Content-Type: application/json' header.
Your api is CORS compliant out of the box, so it's capable of cross-domain communication.

Try with curl:

#GET
curl http://localhost:9001/api/v1/notes -H 'Content-Type: application/json' -w "\n"

#POST (insert)
curl -X POST http://localhost:9001/api/v1/notes -d '{"note":"Hello World!"}' -H 'Content-Type: application/json' -w "\n"

#POST (update)
curl -X POST http://localhost:9001/api/v1/notes/1 -d '{"note":"Uhauuuuuuu!"}' -H 'Content-Type: application/json' -w "\n"

#DELETE
curl -X DELETE http://localhost:9001/api/v1/notes/1 -H 'Content-Type: application/json' -w "\n"

####What's under the hood
Take a look at the source code, it's self explanatory :)
More documentation and info about the code will be available soon.
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions src/App/Controllers/NotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ public function delete($id)

public function getDataFromRequest(Request $request)
{
$payload = json_decode($request->getContent());

$note = array(
"note" => $payload->note,
return $note = array(
"note" => $request->request->get("note")
);
return $note;
}
}
27 changes: 27 additions & 0 deletions src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Silex\Provider\ServiceControllerServiceProvider;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\ServicesLoader;
use App\RoutesLoader;
use Carbon\Carbon;
Expand All @@ -15,6 +16,32 @@

define("ROOT_PATH", __DIR__ . "/..");

//handling CORS preflight request
$app->before(function (Request $request) {
if ($request->getMethod() === "OPTIONS") {
$response = new Response();
$response->headers->set("Access-Control-Allow-Origin","*");
$response->headers->set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
$response->headers->set("Access-Control-Allow-Headers","Content-Type");
$response->setStatusCode(200);
$response->send();
}
}, Application::EARLY_EVENT);

//handling CORS respons with right headers
$app->after(function (Request $request, Response $response) {
$response->headers->set("Access-Control-Allow-Origin","*");
$response->headers->set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
});

//accepting JSON
$app->before(function (Request $request) {
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : array());
}
});

$app->register(new ServiceControllerServiceProvider());

$app->register(new DoctrineServiceProvider(), array(
Expand Down

0 comments on commit b2687e1

Please sign in to comment.