Symfony2 REST API

Posted on: 2014-11-15 | Categories: PHP

bc_circle_symfony2In my recent project i had great opportunity to work together with powerful Symfony2 and amazing ExtJS5 frameworks. In the next few entries i will try to show you how to setup and configure Symfony2 application to serve REST API, than how to build ExtJS5 and AngularJS UI that will communicate with our REST API and finally how to setup basic authentication for REST API application… and maybe more.

Setup and configure Symfony2

Let’s start brand new Symfony 2.5 project (more information can be found on Installing and Configuring Symfony2 page):

Don’t you have Composer installed yet?

Composer is the package manager used by modern PHP applications and the only recommended way to install Symfony2. To install Composer execute the following commands:

During the installation you will be asked for application database configuration – please provide proper parameters. For mailer configuration confirm default settings.

Notice: you will be also asked if you want to generate Acme demo bundle but we do not need it in our project.

JMSSerializerBundle

At the beginning we need to install JMSSerializerBundle for entity serialization (to format such as JSON, XML, or YAML) and to be able to use some extra annotations like @ExclusionPolicy, @Exclude, @Expose or @Type. Full feature list and documentation can be found on bundle official website.

and enable it in app/AppKernel.php:

No extra configuration is necessary.

FOSRestBundle

FOSRestBundle provides various tools to rapidly develop RESTful API’s & applications with Symfony2 framework. We will use this bundle to simplify the process of creating REST API.

Install FOSRestBundle bundle:

Next step is to enable installed bundle – add the following line to AppKernel.php:

set required configuration:

and that’s it… for now.

Important: It is strongly advised to look over the 6 sections of documentation to have better understanding of core concepts

Resource

As REST is resource oriented (Resource-Oriented Architecture) – we need at least one resource for our example, e.g. User resource.

Notice: The web itself is a RESTful service. In REST architecture almost everything is a resource, e.g. page/image/video/sound is a resource.

First we will create Level7 UserBundle to store our REST API logic:

Notice: DefaultController file can be deleted as we won’t need it.

… then we can generate empty User entity to define our resource – empty because in the next step we are going to extend our resource class with FOSUserBundle/Model/User.

FOSUserBundle

As in the near future we are going to implement some basic auth for API, this is a good place to install FOSUserBundle for better user management and some other extra features.

According to FOSUserBundle documentation:

Enable it in AppKernel.php:

Some required configuration app/config/config.yml:

and app/config/security.yml:

Finally extend Level7UserBundle/Entity/Usert with FOSUserBundle/Model/User (aliased as BaseUser):

Notice: --env=prod parameter is not necessary but i just wanted to skip all DEV (development environment) routes.

UsersController: get users / user

First, we need to generate UsersController controller using Symfony2 command line tool (or add it manually):

After that we need to add routing configuration to app/config/routing.yml file:

We haven’t add any logic to UserController so far. Lets add logic for two methods:

  • getUsersAction will return all users
  • getUserAction will return given user by id

Notice: Creating and deleting users will be introduced in next entry.

This is really simple example but can be extended. What is more important, we don’t have to use any special code formatters or serializers to return proper JSON responses – thanks to FOSRestBundle listeners that we configured in app/config/config.yml file.

Testing Rest Api

The best way to verify that everything works as expected is to write some tests (recommended way) – in this example we are not going to write any tests. Instead, we will use great Google Chrome extension: Advanced Rest Client application to send some request and verify responses.

But before that let’s add some users to database – we can do that with fos:user:create interactive command form shell:

After that we can make some test request with installed Advanced Rest Client – if you have no virtual hosts configured, our REST API should be available on url http://localhost/sfRest/web/ + resource routes according to implemented methods :

rest - advanced client 1

rest - advanced client 2

rest - advanced client 3

What’s next?

In the next entry we are going to implement postUsersAction, putUserAction, deleteUserAction and also add some extra features like excluding / exposing entity properties or generating API doc.

The complete source code can be found on GitHub.