Custom Route Name in FOSRESTBundle

I recently had an issue where I wanted to have a controller called SocialMediaProfilesController, but whatever I tried, the automatically generated route names kept coming out as e.g. get_socialmedia_profiles.

I haven’t dived deep into the code to determine as to why this weird spacing issue is occurring, but my guess would be that if you created a controller called SomeEvenLongerStringController, your action might be get_someevenlonger_string. But as I say, I haven’t tried this, so it’s just a guess as to what might be happening.

Anyway, the fix is pretty simple. It’s probably out there on Google, or in the docs, or something, but I couldn’t find it. Instead, I stumbled upon this and it works, so here we go:

<?php

namespace AppBundle\Controller;

use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Routing\ClassResourceInterface;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
// some other use statements here, but these are the interesting ones 

/**
 * Class SocialMediaProfilesController
 * @package AppBundle\Controller
 * @Annotations\RouteResource("socialmediaprofiles")
 */
class SocialMediaProfilesController extends FOSRestController implements ClassResourceInterface
{

By using the RouteResource annotation, you can control how the route names will be generated.

Without the annotation:

  cget_accounts_socialmedia_profiles     GET      ANY      ANY    /accounts/{accountId}/social-media-profiles
  get_accounts_socialmedia_profiles      GET      ANY      ANY    /accounts/{accountId}/social-media-profiles/{socialMediaProfileId}

And then with:

  cget_accounts_socialmediaprofiles     GET      ANY      ANY    /accounts/{accountId}/social-media-profiles
  get_accounts_socialmediaprofiles      GET      ANY      ANY    /accounts/{accountId}/social-media-profiles/{socialMediaProfileId}

So that’s pretty handy.

FOSRESTBundle for REST API

FOSRESTBundle is a tool to help you in the job of creating a REST API with Symfony2. Let’s take a closer look at what it all really means and where to download it and get started.

What is FOS?

FOS stands for Friends of Symfony. It’s a group of people who begun collaborating on the KNP Labs User Bundle. They decided to create a dedicated space for Bundles that they maintained as a group. Over time this has led to many more popular bundles being created via the group.

Check out the Friends of Symfony GitHub to see the bundles available.

What is REST?

REST stands for Representational State Transfer. It is resource-based rather than action-based. In a RESTful API, we’re talking about things instead of actions.

REST typically runs over HTTP (Hypertext Transfer Protocol) and has several architectural constraints:

It decouples consumers from producers.

It leverages a layered system.

It leverages a uniform interface.

Able to leverage a cache.

Stateless existence.

Features of the FOSRESTBundle

The FOSRESTBundle gives us great tools to allow the quick development of RESTful API’s and applications using Symfony2. Once you have confidence in using it, you’ll quickly find so many other possibilities become available including apps, Angular JS front-ends and as well as other opportunities.

Some key features include:

  • Generate URLs following REST conventions using a custom route loader.
  • Accept header format negotiation.
  • It has an exception controller used for sending HTTP status codes.
  • RESTful decoding of HTTP request body and accept headers.
  •  View layer for allowing output and format agnostic controllers.

Benefits of FOSRESTBundle

REST is likely to keep growing as more and more businesses seek open, well-defined interfaced for developing applications and infrastructure services. It’s very useful and worthwhile to learn.

Advantages of learning REST include:

  • It is designed for using over Open Internet/Web. It’s a better choice for web scale applications and cloud-based platforms compared to SOAP (Simple Object Access Protocol) and is often the choice for the architecture of internet services these days.
  • RESTful web services are easily leveraged by most tools.
  • REST forbids conversational state, meaning we can scale very wide with the addition of server nodes behind a load balancer.

Getting Started with FOSRESTBundle:

You can find the FOSRESTBundle here. Installation is a quick one step process. There are six main sections on the download page to look over before you begin. Check out the config references too, and there are also some example applications too that can be used as a guideline.

FOSRESTBundle

Ready to learn to code a RESTful API?

In our RESTful API course using FOSRESTBundle, you can watch and learn how to set up, configure and implement a REST API. Sometimes it’s easier watching than trying to figure stuff out for yourself, plus you can also ask questions!

The course covers all of the basics such as GET, POST, PUT and DELETE. This is along with handling related data collections, leveraging the Symfony2 Forms component, and very importantly, this is all done using test driven development techniques. Let’s get cracking and we hope to see you on the inside!