// ... // --> don't forget this new use statement use Symfony\Component\HttpFoundation\JsonResponse; // ... /** * @Route("/api/lucky/number") */ public function apiNumberAction() { $data = array( 'lucky_number' => rand(0, 100), ); // calls json_encode and sets the Content-Type header return new JsonResponse($data); } // ...