Create REST api in Symfony

Create REST api in Symfony

A) Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require friendsofsymfony/rest-bundle

 

B) Enable the Bundle

Then, enable the bundle by adding the following line in the app/AppKernel.php file of your project:

 1

2

3

4

5

6

7

8

9

10

11

12

13

// app/AppKernel.php

class AppKernel extends Kernel

{

public function registerBundles()

{

$bundles = [

// ...

new FOS\RestBundle\FOSRestBundle(),

];

 

// ...

}

}

 

C) Enable a Serializer

This bundle needs a serializer to work correctly. In most cases, you'll need to enable a serializer or install one. This bundle tries the following (in the given order) to determine the serializer to use:

  • The one you configured using service.serializer(if you did).
  • The JMS serializer, if the JMSSerializerBundleis available (and registered).
  • The Symfony Serializerif it's enabled (or any service called serializer).
  1. D) Update config.yml

# FOSRest Configuration

fos_rest:

param_fetcher_listener: true

routing_loader:

default_format: json

view:

view_response_listener: 'force'

formats:

json: true

allowed_methods_listener: true

access_denied_listener:

json: true

format_listener:

rules:

- { path: '^/api', priorities: ['json'], fallback_format: json, }

- { path: '^/', stop: true }