Skip to content

REST APIs

Apodemus Erectus edited this page Jun 10, 2020 · 16 revisions

Disclaimer: In this guide I assume you already know what a REST API is.

The JSONResponse Object

The most basic way to create a API-like response is by just returning a JSONResponse Object. No extra bundles required, just make sure you import the right class.

// Don't forget the import UwU <3
use Symfony\Component\HttpFoundation\JsonResponse;

You may be familiar with the way how Symfony renders a page in your Controller methods. By replacing the $this->render() with a JSONResponse(), you return JSON instead of a page.

// Replace the $this->render() with a JSONResponse()
return $this->render('default/index.html.twig', [
    'data' => 'ok boomer'
]);
// An example JSONResponse
return new JsonResponse(
    [
        "status" => "200",
        "data" => "My name is Yoshikage Kira. I'm 33 years old."
    ],
    200
);

As you can see the $this->render() and the JSONResponse() look very similar and they are: they both have an array that should contain the data you want to display. The difference is that you don't feed the data to a twig template, but instead the JSONResponse converts all the data in the array to JSON.

Remember the JSONResponse from above? this is what it should output:

{
    "status": "200",
    "data": "My name is Yoshikage Kira. I'm 33 years old."
}

You don't necessarily have to return a new JSONResponse(), you can create a JSONResponse() earlier in your method and fill it up later with the setData() method.

$response = new JsonResponse();
$response->setData(["data" => 123]);

Of course you can just pass other variables to the JSONResponse() array, if you don't like to call setData() or for any other reason.

$yoshikage = "Killer Queen Bites the Dust!";
return new JsonResponse(
    [
        "status" => "200",
        "data" => $yoshikage
    ],
    200
);

More about JSON Responses

Responding JSON with a regular Response object

If you don't want the extra import or if you have any other reason to not use JSONResponse(), you can use a regular Response(), which is more commonly used in Symfony. Request() objects should come out of the box with Symfony, if your Symfony does not have a Request() object, you're doing something terribly terribly wrong.

// Don't forget to import the Response if you haven't already
use Symfony\Component\HttpFoundation\Response;

I assume you are already quite familiar with a Response(), so here is an example of a Response() that responds JSON.

$response = new Response();
$response->setContent(json_encode([
    "data" => "I, Giorno Giovanna, have a dream.",
]));
$response->headers->set("Content-Type", "application/json");

Requests

What is a REST API if you can't send requests to the API? I tell you, not a very good one.

You know the drill: Make sure you have the right import

// I request you don't mess this up.
use Symfony\Component\HttpFoundation\Request;

Depending on what kind of request data you want to access you'll need to access different Request() attributes, which I'm going to list here and I have shamelessly copied from this page

  • request: POST
  • query: GET
  • cookies: Pretty self-explanatory
  • files: Yea, it's pretty much only request and query that are something different from what they represent
  • server: SERVER
  • headers: HEADERS

So, if we for example want to see the POST data, we access the request method of the Request() object.

// In this example we have the Response() in de method parameters,
// because that's usually how request data drom the client gets accessed inside our controller
public function api(Request $request) [
    // Say, we want to access an id that the client has sent us
    $request->request->get('id');
}

Clone this wiki locally