January 6, 2018



This Post is about how we create a REST API using SYMFONY4 after the changes in structure from version 2 to version 3 ,now we got the big change in structure with the version 4 which all the changes based on folowing the open source ecosystem in file structure and in having a light project to create bare bones applications.

1- Creating the Project

So we start by running :

composer create-project "symfony/skeleton:^4.0" REST-API-SY4

to create our project using symfony/skeleton, next we test if it's installed correctly by runing inside the project:
php -S -t public

now we have the app running on

2- Install dependency

To create the REST API we start by installing the needed bundles.

a- Install SensioFrameworkExtraBundle

This bundle contain the extra features that make our code experience easy like annotation ...
so we run :
composer require sensio/framework-extra-bundle

b- Install jms/serializer-bundle

This Bundle will help as to serialize or deserialize the resources of our application.
so we run :
composer require jms/serializer-bundle

c- Install friendsofsymfony/rest-bundle

This bundle help us in creating the a REST api with Symfony framework
so we run :
composer require friendsofsymfony/rest-bundle

The FosRestBundle is incompatible with Sensio ExtraBundle So we need to add this configration to define the view listener in the services.yaml file.

d- Install symfony/orm-pack

This Bundle is for the Doctrine ORM integration
so we run :
composer require symfony/orm-pack
now we set the configuration for our data base in the configuration file .env by changing the db_name the user and the password

As result for the installation we have all the bundles added in Rest-API-SF4/config/bundles.php

the configuration files of the added bundels is under config/packages directories of our project.


buy the way the extention is no more yml for the yaml files ;) it's yaml.

3- Add The ressources for test

we will add an entity 'ARTICLE' for our test under app/Entity:

next we run this commands:
=> for database creation
bin/console doctrine:database:create
=> to create the database structure
bin/console doctrine:schema:create

4- Create the Rest API

The creation of our rest api will be simple without validation the main reason was to create the restAPI using SF4 :) .

a- Create method (POST)

The method treat sended parameters to create a new instance of article class next it persist the object in our database.

finally a succes response with stauts code 201 and created ressource.


b- Get method (GET)

This method select all resources of articles in Data base.


4- Github Project


The Project REPO

Enjoy ^_^ !!